It's no secret that Telegram is currently one of the most popular messengers. Especially among IT professionals. It is user-friendly, has no embedded ads and works very stable. Quite most of the time I communicate both on work and on personal issues in this messenger. Therefore, one day I thought that it would be convenient so that in the same messenger I could receive notifications about the operation of some of my services. At that time, I was just actively working on integrating the // devdigest and Telegram project , so using the same native Telegram Bot SDK I quickly implemented the logger.
A few days ago I decided to return to this project, slightly clean up and refactor the code, and then make it publicly available - maybe the ability to get logs in Telegram will be useful to someone else.
Training
Before moving on to configuring the logger itself, you will need to complete a few preliminary steps. Namely - create a channel (public or private) where logs will be displayed and create a bot in telegrams, through which the process of publishing logs will be implemented.
Telegram, . , Telegram . , , , .
. - . - .
.
โ @JsonDumpBot. . :
{
"update_id": 111001100,
"message": {
"message_id": 123456,
"from": {
"id": 12345678,
"is_bot": false,
"first_name": "FirstName",
"username": "username",
"language_code": "en"
},
"chat": {
"id": 123456,
"first_name": "FirstName",
"username": "username",
"type": "private"
},
"date": 1111111111,
"forward_from_chat": {
"id": -1123456789101,
"title": "torf.tv logs",
"type": "channel"
},
"forward_from_message_id": 1,
"forward_date": 1111111111,
"text": "test"
}
}
forward_from_chat -> id
, .
TelegramLoggerOptions, :
AccessToken โ ;
ChatId โ (, ), , ;
LogLevel โ , . Warning, Error;
Source โ . , ;
โ , .
TelegramLoggerOptions.
var options = new TelegramLoggerOptions
{
AccessToken = "1234567890:AAAaaAAaa_AaAAaa-AAaAAAaAAaAaAaAAAA",
ChatId = "-0000000000000",
LogLevel = LogLevel.Information,
Source = "Human Readable Project Name"
};
- AddTelegram():
builder .ClearProviders() .AddTelegram(options) .AddConsole();
appconfig.json
, :
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"Telegram": {
"LogLevel": "Warning",
"AccessToken": "1234567890:AAAaaAAaa_AaAAaa-AAaAAAaAAaAaAaAAAA",
"ChatId": "@channel_name",
"Source": "Human Readable Project Name"
}
},
"AllowedHosts": "*"
}
Next, an IConfiguration instance must be passed to the AddTelegram () extension method,
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((context, builder) =>
{
if (context.Configuration != null)
builder
.AddTelegram(context.Configuration)
.AddConsole();
})
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<startup>(); });
An example is here
Installation
You can install the logger from NuGet , or integrate the code directly into your project. The library is distributed under the MIT license .