-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quickstart configuration
- Loading branch information
Showing
15 changed files
with
148 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Serilog" Version="3.1.1" /> | ||
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\X.Serilog.Sinks.Telegram\X.Serilog.Sinks.Telegram.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,22 @@ | ||
using Serilog; | ||
using Serilog.Events; | ||
using X.Serilog.Sinks.Telegram.Extensions; | ||
|
||
const string botToken = "TELEGRAM_BOT_TOKEN"; | ||
const string loggingChatId = "CHANNEL_OR_CHAT_ID"; | ||
|
||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.TelegramCore( | ||
token: botToken, | ||
chatId: loggingChatId, | ||
logLevel: LogEventLevel.Verbose) | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
|
||
|
||
while (true) | ||
{ | ||
var level = Random.Shared.NextInt64(0, 6); | ||
Log.Logger.Write((LogEventLevel)level, "Message"); | ||
await Task.Delay(500); | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/X.Serilog.Sinks.Telegram/Extensions/DependencyInjectionExtensions.cs
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,58 @@ | ||
using System.Collections.Immutable; | ||
using X.Serilog.Sinks.Telegram.Batch.Rules; | ||
using X.Serilog.Sinks.Telegram.Configuration; | ||
using X.Serilog.Sinks.Telegram.Filters; | ||
|
||
namespace X.Serilog.Sinks.Telegram.Extensions; | ||
|
||
public static class DependencyInjectionExtensions | ||
{ | ||
public static LoggerConfiguration TelegramCore( | ||
this LoggerSinkConfiguration sinkConfig, | ||
string token, | ||
string chatId, | ||
LogEventLevel logLevel | ||
) | ||
{ | ||
ArgumentNullException.ThrowIfNull(sinkConfig); | ||
ArgumentNullException.ThrowIfNull(token); | ||
ArgumentNullException.ThrowIfNull(chatId); | ||
|
||
return TelegramCoreInternal(sinkConfig, token, chatId, logLevel); | ||
} | ||
|
||
private static LoggerConfiguration TelegramCoreInternal( | ||
LoggerSinkConfiguration sinkConfig, | ||
string token, | ||
string chatId, | ||
LogEventLevel logLevel) | ||
{ | ||
return sinkConfig.Telegram(config => | ||
{ | ||
config.Token = token; | ||
config.ChatId = chatId; | ||
config.Mode = TelegramSinkDefaults.DefaultFormatterMode; | ||
config.BatchPostingLimit = TelegramSinkDefaults.BatchPostingLimit; | ||
config.BatchEmittingRulesConfiguration = new BatchEmittingRulesConfiguration | ||
{ | ||
RuleCheckPeriod = TelegramSinkDefaults.RulesCheckPeriod, | ||
BatchProcessingRules = new List<IRule> | ||
{ | ||
new BatchSizeRule(config.LogsAccessor, batchSize: config.BatchPostingLimit), | ||
// send logs to the Telegram once per 250 seconds | ||
new OncePerTimeRule(TelegramSinkDefaults.RulesCheckPeriod * 50) | ||
}.ToImmutableList() | ||
}; | ||
config.FormatterConfiguration = TelegramSinkDefaults.DefaultFormatterConfiguration; | ||
config.LogFiltersConfiguration = new LogsFiltersConfiguration() | ||
{ | ||
ApplyLogFilters = false, | ||
Filters = new ImmutableArray<IFilter>() | ||
}; | ||
}, | ||
messageFormatter: null!, | ||
restrictedToMinimumLevel: logLevel); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
.../LoggerConfigurationTelegramExtensions.cs → .../LoggerConfigurationTelegramExtensions.cs
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
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