forked from a-gubskiy/X.Extensions.Logging.Telegram
-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
37 IMessageFormatter and other fixes #38
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
69a3cdc
Restore README.md file
a-gubskiy 1839068
Update documentation
a-gubskiy 699b206
Update example.
a-gubskiy 99d447d
Update version
a-gubskiy 8f73667
Update project settings
a-gubskiy 041547c
Fix package references
a-gubskiy f48517d
Merge branch 'main' into 37-imessageformatter
a-gubskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
## X.Extensions.Logging.Telegram | ||
[![NuGet](https://img.shields.io/nuget/v/X.Extensions.Logging.Telegram)](https://www.nuget.org/packages/X.Extensions.Logging.Telegram) | ||
[![NuGet Downloads](https://img.shields.io/nuget/dt/X.Extensions.Logging.Telegram)](https://www.nuget.org/packages/X.Extensions.Logging.Telegram) | ||
|
||
X.Extensions.Logging.Telegram is logging provider for standard .NET logging. | ||
|
||
### Getting Started | ||
|
||
You can configure Telegram logging provider by code or by config file: | ||
|
||
```csharp | ||
var options = new TelegramLoggerOptions(LogLevel.Information) | ||
{ | ||
AccessToken = "1234567890:AAAaaAAaa_AaAAaa-AAaAAAaAAaAaAaAAAA", | ||
ChatId = "-0000000000000", | ||
Source = "Human Readable Project Name" | ||
}; | ||
|
||
... | ||
|
||
builder | ||
.ClearProviders() | ||
.AddTelegram(options) | ||
.AddConsole(); | ||
|
||
``` | ||
|
||
### appconfig.json | ||
|
||
``` | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
}, | ||
"Telegram": { | ||
"LogLevel": { | ||
"Default": "Error", | ||
"WebApp.Controllers": "Warning" | ||
}, | ||
"AccessToken": "1234567890:AAAaaAAaa_AaAAaa-AAaAAAaAAaAaAaAAAA", | ||
"ChatId": "1234567890", | ||
"Source": "Human Readable Project Name" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} | ||
``` | ||
|
||
and pass IConfiguration object to extensions 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>(); }); | ||
```` | ||
|
||
### Use a custom log writer | ||
Now developers can use their own implementation for writing data to Telegram. Custom writer should implement _ILogWriter_ interface: | ||
|
||
``` cs | ||
var customLogWriter = new CustomLogWriter(); | ||
logBuilder.AddTelegram(options, customLogWriter); | ||
``` | ||
|
||
### Use custom message formatter | ||
For implement custom message formatting _ITelegramMessageFormatter_ can be used now. | ||
|
||
``` cs | ||
private ITelegramMessageFormatter CreateFormatter(string name) | ||
{ | ||
return new CustomTelegramMessageFormatter(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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,10 @@ | |
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="../../README.md" Pack="true" PackagePath=""/> | ||
<None Include="README.md" Pack="true" PackagePath=""/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can simply use a single |
||
<None Include="../../LICENSE.md" Pack="true" PackagePath=""/> | ||
<None Include="../../x-extensions.png" Pack="True" PackagePath=""/> | ||
</ItemGroup> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ public static class LoggerConfigurationTelegramExtensions | |
public static LoggerConfiguration Telegram( | ||
this LoggerSinkConfiguration loggerConfiguration, | ||
Action<TelegramSinkConfiguration> configureAction, | ||
IMessageFormatter messageFormatter, | ||
IMessageFormatter? messageFormatter, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amazing. That's an important fix as some users already had issues with that! |
||
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum) | ||
{ | ||
if (loggerConfiguration == null) | ||
|
@@ -34,12 +34,8 @@ public static LoggerConfiguration Telegram( | |
configureAction(config); | ||
config.Validate(); | ||
|
||
return loggerConfiguration.Sink( | ||
new TelegramSink( | ||
channel.Writer, | ||
logsAccessor, | ||
config, | ||
messageFormatter), | ||
restrictedToMinimumLevel); | ||
var sink = new TelegramSink(channel.Writer, logsAccessor, config, messageFormatter); | ||
|
||
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# X.Extensions.Serilog.Sinks.Telegram | ||
|
||
[![NuGet](https://img.shields.io/nuget/v/X.Extensions.Serilog.Sinks.Telegram)](https://www.nuget.org/packages/X.Extensions.Serilog.Sinks.Telegram) | ||
[![NuGet Downloads](https://img.shields.io/nuget/dt/X.Extensions.Serilog.Sinks.Telegram)](https://www.nuget.org/packages/X.Extensions.Serilog.Sinks.Telegram) | ||
|
||
|
||
X.Serilog.Sinks.Telegram is an open-source Serilog sink that allows you to send log events to Telegram. It's a convenient way to integrate Telegram as a logging output, enabling you to receive important log information directly in your chat. | ||
|
||
## Features | ||
|
||
- **Real-time Logging**: The sink offers the ability to send log events to a Telegram channel in real-time, ensuring that you can stay up-to-date with your application's behavior and any issues as they arise. | ||
|
||
- **Customizable Formatting**: You can configure the format of log messages sent to the Telegram channel, allowing you to tailor them to your preferences and specific requirements. | ||
|
||
- **Filtering**: The sink supports filtering log events before they are dispatched to the Telegram channel, ensuring that only pertinent information is shared. | ||
|
||
- **Asynchronous Sending**: Log events are sent asynchronously to the Telegram channel, minimizing potential impact on your application's performance. | ||
|
||
- **Easy Configuration**: Configuring the sink to work with your Telegram channel is straightforward, and you can find comprehensive information in the [Configuration Wiki](https://github.com/Bardin08/X.Serilog.Sinks.Telegram/wiki/Configuration). | ||
|
||
## Getting Started | ||
|
||
To begin using the X.Serilog.Sinks.Telegram sink, follow these steps: | ||
|
||
1. **Install the Package**: You can install the sink package from NuGet using the following command: | ||
```shell | ||
dotnet add package X.Serilog.Sinks.Telegram | ||
``` | ||
|
||
2. **Configure the Sink**: Set up the Telegram sink with the appropriate settings in your application's configuration. Here's an example configuration in C#: | ||
|
||
```c# | ||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.TelegramCore( | ||
token: botToken, | ||
chatId: loggingChatId, | ||
logLevel: LogEventLevel.Verbose) | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
``` | ||
|
||
3. **Start Logging**: Once the sink is configured, you can log in using Serilog as usual. Log events will be sent to your Telegram channel. | ||
|
||
For more detailed configuration options, please refer to the [Configuration Wiki](https://github.com/Bardin08/X.Serilog.Sinks.Telegram/wiki/Configuration). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just an overload of
Configure(...)
?IMO, no sense to produce additional cognitive complexity for users