Skip to content

Configuration

Vladyslav Bardin edited this page Feb 19, 2024 · 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. LogQueryBuilder
  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 info about all notifications 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 log filters.

Property Type Description
ApplyLogFilters bool Indicates whether to apply log filters.
LogQueryBuilder LogQueryBuilder List of filters to be applied.

LogQueryBuilder

Fluent API-based filter builder. This allows to configure log filtering based on the convenient API.

TelegramSinkDefaults

Contains the default configuration values for the Telegram Sink.

Usage Examples

You can configure Telegram sink with 2 methods. For quickstart, you can use the TelegramCore method. Also, you can use the Telegram method for advanced configuration.

For a quickstart you can use the TelegramCore method

Log.Logger = new LoggerConfiguration()
    .WriteTo.TelegramCore(
        token: botToken,
        chatId: loggingChatId,
        logLevel: LogEventLevel.Verbose)
    .WriteTo.Console()
    .CreateLogger();

Here is an example of how to configure the Telegram Sink using advanced configuration, 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 a 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 per your requirements. Replace your_telegram_bot_token and your_chat_id with your Telegram bot token and chat ID.

Important: Always 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