.NET Logging Provider for Telegram — now Open and Extensible – Telegram Group

.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.

Ten articles before and after

Refer a friend to Opulous for #OpulXmas – Telegram Group

Mercor Finance Kicks Off Full Launch Month — Introducing Its Official Telegram Bot – Telegram Group

Telegram is the Mos Eisley Cantina (Tatooine) of the Cryptoverse – Telegram Group

How to anonymously follow Instagram posts, stories and highlights via Telegram – Telegram Group

We Need to Change How We Talk About Technology – Telegram Group

Are you getting the message already? – Telegram Group

HODLing in Bear Market (DAO). “When the going gets tough , the though… – Telegram Group

Unmarshal Telegram Genie Bot. Unmarshal is proud to release the… – Telegram Group

5 Reasons Why You Should Change WhatsApp for Telegram – Telegram Group

How to keep content in Telegram channels ‘members-only’ – Telegram Group