Skip to content

Configuration

Vladyslav Bardin edited this page Aug 11, 2023 · 6 revisions

Documentation: Telegram Sink Configuration Settings

In the context of the Telegram Sink for logging, several configuration classes and enums are available. These classes provide a way to configure the sink according to specific needs.

Table of Contents

  1. TelegramSinkConfiguration
  2. LoggingMode
  3. BatchEmittingRulesConfiguration
  4. FormatterConfiguration
  5. LogsFiltersConfiguration
  6. LogFiltersOperator
  7. TelegramSinkDefaults
  8. Usage Examples

TelegramSinkConfiguration

This is the primary configuration class which contains settings required for the operation of the Telegram Sink.

Property Type Description
Token string Token for Telegram API access.
ChatId string Identifies the chat to which log messages will be posted.
BatchPostingLimit int The maximum number of events to post in a single batch.
Mode LoggingMode Sets the logging mode.
FormatterConfiguration FormatterConfiguration Configuration for formatting logs.
BatchEmittingRulesConfiguration BatchEmittingRulesConfiguration Configuration for rules on emitting batches.
LogFiltersConfiguration LogsFiltersConfiguration Configuration for filtering logs.

LoggingMode

An enumeration that allows choosing between two ways of logging.

A. Logs: Log messages will be published to the specified Telegram channel.
B. AggregatedNotifications: Messages will contain an info about all notifications which were received during a batch period or batch limit.

BatchEmittingRulesConfiguration

Configuration class for managing batch emitting rules.

Property Type Description
RuleCheckPeriod TimeSpan Check period for batch emit rules.
BatchProcessingRules IImmutableList The batch processing rules to be applied.

FormatterConfiguration

Settings for formatting log messages.

Property Type Description
UseEmoji bool Indicates whether to use emojis in the output.
ReadableApplicationName string User-friendly name of the application.
IncludeException bool Indicates whether to include exception details in the output.
IncludeProperties bool Indicates whether to include property details in the output.
TimeZone TimeZoneInfo? Indicates which time zone to use in the output timestamp. If null, server time will be used.

LogsFiltersConfiguration

Configuration class for applying logs filters.

Property Type Description
ApplyLogFilters bool Indicates whether to apply log filters.
FiltersOperator LogFiltersOperator Operator used for combining filters.
Filters IImmutableList List of filters to be applied.

LogFiltersOperator

An enumeration that allows choosing a logical operator for combining filters.

A. And: All filters have to pass.
B. Or: At least one of the filters has to pass.

TelegramSinkDefaults

Contains the default configuration values for the Telegram Sink.

Usage Examples

Here is an example of how to configure the Telegram Sink using an extension method on the LoggerConfiguration class:

var logger = new LoggerConfiguration()
    .Telegram(config =>
    {
        config.Token = "your_telegram_bot_token";
        config.ChatId = "your_chat_id";
        config.BatchPostingLimit = 10;
        config.Mode = LoggingMode.Logs;
        config.FormatterConfiguration = new FormatterConfiguration
        {
            UseEmoji = true,
            ReadableApplicationName = "MyTestApp",
            IncludeException = true,
            IncludeProperties = true,
            TimeZone = TimeZoneInfo.Utc
        };
        config.BatchEmittingRulesConfiguration = new BatchEmittingRulesConfiguration
        {
            // Batch Emitting rules configuration here...
        };
        config.LogFiltersConfiguration = new LogsFiltersConfiguration
        {
            ApplyLogFilters = true,
            FiltersOperator = LogFiltersOperator.Or,
            Filters = new List<IFilter> {
                // Your filters here...
            }
        };
    }, null, LogEventLevel.Debug)
    .CreateLogger();

logger.Information("This is a test log message");

This code configures the TelegramSinkConfiguration instance through a delegate and an extension method. This provides more fluent API and makes the configuration step more clear and straightforward. You would add further sinks and then create the logger from your loggerConfiguration.

In this example, new MyCustomMessageFormatter() is used for message formatting. You should replace MyCustomMessageFormatter with your actual implementation of the IMessageFormatter or leave it null to use one of the default one.

Also, add your own batch emitting rules and filters as per your requirements. Make sure to replace your_telegram_bot_token and your_chat_id with your actual Telegram bot token and chat ID.

Important: Always remember to validate the configuration towards the end, before using it to instantiate an object. This is to ensure that the provided settings are valid and appropriate for the operation of the Telegram sink.

Clone this wiki locally