Nerd For Tech
Published in

Nerd For Tech

.NET Logging Provider for Telegram — now Open and Extensible

Some time ago I wrote a publication about .NET Logging Provider for Telegram — a simple library that allows writing logs to Telegram.

Now I want to tell about some improvements which I made. I want to make this library more open and flexible. So I added a lot of options for logging process customization.

Improved log level filtering

First of all, I updated logs level filtering. Telegram Logger now respects log levels for different namespaces.

Previously only the global log level for Telegram Logger can be configured via code:

var options = new TelegramLoggerOptions
{
...
LogLevel = LogLevel.Information,
...
};

or via appsettings.json:

{
"Logging": {
"LogLevel": {
...
},
"Telegram": {
"LogLevel": "Warning",
...
}
},
...
}

Now logger configuration can be done more conveniently as described in Logging in .NET Core and ASP.NET Core.

Configration via appsettings.json file:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"Telegram": {
"LogLevel": {
"Default": "Error",
"WebApp.Controllers": "Warning"
}
,
...
}
},
...
}

The LogLevel specifies the minimum level to log for selected categories. When a LogLevel is specified, logging is enabled for messages at the specified level and higher. For more information, see Log levels on the Microsoft Docs portal.

Use custom log writer

Now developers can use their own implementation for writing data to Telegram. Custom writer should implement ILogWriter interface:

var customLogWriter = new CustomLogWriter();
logBuilder.AddTelegram(options, customLogWriter);

Use custom message formatter

For implement custom message formatting ITelegramMessageFormatter can be used now.

private ITelegramMessageFormatter CreateFormatter(string name)
{
return new CustomAceTelegramMessageFormatter(name);
}
logBuilder..AddTelegram(options, CreateFormatter)

For using custom message formatter delegate Func<string, ITelegramMessageFormatter> should be passed to extensions method AddTelegram. Delegate should be used because formatter needs to know which category is used for rendering the message.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Gubskiy

Andrew Gubskiy

131 Followers

Software Engineer, Ph.D., Microsoft MVP in Developer Technologies.