Skip to content

Commit cc38edf

Browse files
committed
Add timezones support and update readme
1 parent f50c5e2 commit cc38edf

File tree

5 files changed

+123
-58
lines changed

5 files changed

+123
-58
lines changed

README.md

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,90 @@
11
# X.Serilog.Sinks.Telegram
2-
X.Serilog.Sinks.Telegram is a Serilog sink that write events to [Telegram](https://telegram.org/) channel or chat.
32

4-
## Current Statuses
53
[![Build status](https://ci.appveyor.com/api/projects/status/n4uj9qfuywrkdrhb/branch/main?svg=true)](https://ci.appveyor.com/project/Bardin08/x-serilog-sinks-telegram/branch/main)
6-
[![NuGet Badge](https://buildstats.info/nuget/X.Serilog.Sinks.Telegram)](https://www.nuget.org/packages/X.Serilog.Sinks.Telegram/)
74

8-
## Installation
5+
[![NuGet](https://img.shields.io/nuget/v/X.Serilog.Sinks.Telegram)](https://www.nuget.org/packages/X.Serilog.Sinks.Telegram)
6+
[![NuGet Downloads](https://img.shields.io/nuget/dt/X.Serilog.Sinks.Telegram)](https://www.nuget.org/packages/X.Serilog.Sinks.Telegram)
97

10-
The package can be installed by NuGet manually or within one of the listed commands.
8+
## Table of Contents
119

12-
```ps
13-
PM> Install-Package X.Serilog.Sinks.Telegram -Version 1.0.0
14-
```
10+
- [X.Serilog.Sinks.Telegram](#xserilogsinkstelegram)
11+
- [Introduction](#introduction)
12+
- [Features](#features)
13+
- [Getting Started](#getting-started)
14+
- [Examples](#examples)
15+
- [Contributing](#contributing)
16+
- [License](#license)
1517

16-
```sh
17-
$ dotnet add package X.Serilog.Sinks.Telegram --version 1.0.0
18-
```
18+
## Introduction
1919

20-
## Documentation
21-
For more comprehensive and detailed documentation, please visit our [GitHub Wiki](https://github.com/Bardin08/X.Serilog.Sinks.Telegram/wiki/Overview) pages. Here, you'll find in-depth information about configuration options, usage examples, troubleshooting guides, and more to help you effectively integrate and utilize the X.Serilog.Sinks.Telegram sink in your projects.
20+
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.
2221

22+
## Features
2323

24-
## Usage
24+
- **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.
2525

26-
Check [docs](./docs) folder to find more interesting things that can be useful.
26+
- **Customizable Formatting**: You have the flexibility to configure the format of log messages sent to the Telegram channel, allowing you to tailor them to your preferences and specific requirements.
2727

28-
### Code-Based Configuration
29-
The simplest configuration require [Telegram bot token](https://core.telegram.org/bots#generating-an-authentication-token) and [channel ID](https://community.jamaicans.dev/t/get-the-telegram-channel-id/427). Check the enclosed links to understand how to receive them.
28+
- **Filtering**: The sink supports filtering log events before they are dispatched to the Telegram channel, ensuring that only pertinent information is shared.
3029

31-
```cs
32-
Log.Logger = new LoggerConfiguration()
33-
.WriteTo.Telegram(
34-
token: "0000000000:0000_000000000000000000000000000000",
35-
chatId: "-0000000000000")
36-
.CreateLogger();
37-
```
30+
- **Asynchronous Sending**: Log events are sent asynchronously to the Telegram channel, minimizing any potential impact on your application's performance.
3831

39-
For more complex configuration examples please check the related wiki page or browse the examples folder.
32+
- **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).
4033

41-
Sink also can be configured by JSON configuration file.
42-
### JSON-Based Configuration
43-
#### *Microsoft.Extensions.Configuration* package required
44-
Keys and values are case-insensitive.
34+
## Getting Started
35+
36+
To begin using the X.Serilog.Sinks.Telegram sink, follow these steps:
37+
38+
1. **Install the Package**: You can install the sink package from NuGet using the following command:
39+
```shell
40+
dotnet add package X.Serilog.Sinks.Telegram
41+
```
4542

46-
```json
47-
"Serilog": {
48-
"Using": [
49-
"X.Serilog.Sinks.Telegram"
50-
],
51-
"WriteTo": [
43+
2. **Configure the Sink**: In your application's configuration, set up the Telegram sink with the appropriate settings. Here's an example configuration in C#:
44+
45+
```c#
46+
var logger = new LoggerConfiguration()
47+
.Telegram(config =>
5248
{
53-
"Name": "Telegram",
54-
"Args": {
55-
"Token": "0000000000:0000_000000000000000000000000000000",
56-
"ChatId": "-0000000000000",
57-
}
58-
}
59-
],
60-
}
49+
config.Token = "your_telegram_bot_token";
50+
config.ChatId = "your_chat_id";
51+
config.BatchPostingLimit = 10;
52+
config.Mode = LoggingMode.Logs;
53+
config.FormatterConfiguration = new FormatterConfiguration
54+
{
55+
UseEmoji = true,
56+
ReadableApplicationName = "MyTestApp",
57+
IncludeException = true,
58+
IncludeProperties = true
59+
};
60+
config.BatchEmittingRulesConfiguration = new BatchEmittingRulesConfiguration
61+
{
62+
// Batch Emitting rules configuration here...
63+
};
64+
config.LogFiltersConfiguration = new LogsFiltersConfiguration
65+
{
66+
ApplyLogFilters = true,
67+
FiltersOperator = LogFiltersOperator.Or,
68+
Filters = new List<IFilter> {
69+
// Your filters here...
70+
}
71+
};
72+
}, null, LogEventLevel.Debug)
73+
.CreateLogger();
6174
```
6275

63-
## Roadmap
64-
Project's roadmap described at [Roadmap](./docs/roadmap.md).
76+
3. **Start Logging**: Once the sink is configured, you can start logging using Serilog as you normally would. Log events will be sent to your Telegram channel.
77+
78+
For more detailed configuration options, please refer to the [Configuration Wiki](https://github.com/Bardin08/X.Serilog.Sinks.Telegram/wiki/Configuration).
79+
80+
## Examples
81+
82+
This repository includes a number of example projects that demonstrate how to use X.Serilog.Sinks.Telegram in various scenarios. These examples can be very helpful if you're just getting started or looking to use a specific feature.
83+
84+
You can find the examples in the following location: [X.Serilog.Sinks.Telegram Examples](https://github.com/Bardin08/X.Serilog.Sinks.Telegram/tree/main/examples)
6585

6686
## Contributing
6787
Feel free to add any improvements you want via pull requests. All pull requests must be linked to an issue.
6888

89+
## License
90+
This project is licensed under the [MIT License](https://en.wikipedia.org/wiki/MIT_License)https://en.wikipedia.org/wiki/MIT_License.

examples/WebApp/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
{
4040
UseEmoji = true,
4141
ReadableApplicationName = "WebApp Example",
42-
IncludeException = true,
43-
IncludeProperties = true
42+
IncludeException = false,
43+
IncludeProperties = false,
44+
TimeZone = TimeZoneInfo.Utc
4445
};
4546
config.BatchPostingLimit = 10;
4647
config.Mode = LoggingMode.Logs;

src/X.Serilog.Sinks.Telegram/Configuration/FormatterConfiguration.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,52 @@
66
public class FormatterConfiguration
77
{
88
/// <summary>
9-
/// Gets a value indicating whether to use emojis in the output.
9+
/// Determines whether to replace the log level text definition with emojis.
1010
/// </summary>
11+
///
12+
/// <value>
13+
/// A boolean representing the configuration. If true, emojis are used in place of log levels.
14+
/// </value>
1115
public bool UseEmoji { get; init; }
1216

1317
/// <summary>
14-
/// Gets the user-friendly name of the application.
18+
/// Provides a readable name for the application.
1519
/// </summary>
20+
///
21+
/// <value>
22+
/// The application's name. Useful when different applications are sending logs to the same channel. If null, this property will be ignored.
23+
/// </value>
1624
public string? ReadableApplicationName { get; init; }
1725

1826
/// <summary>
19-
/// Gets a value indicating whether to include exception details in the output.
27+
/// Determines the handling of exceptions in log messages.
2028
/// </summary>
29+
///
30+
/// <value>
31+
/// A boolean representing the configuration. If true, and an exception is not null, the exception is included in the log message as a serialized JSON. This setting is only applicable when TelegramSinkConfiguration.Mode is LoggingMode.Logs.
32+
/// </value>
2133
public bool IncludeException { get; init; }
2234

2335
/// <summary>
24-
/// Gets a value indicating whether to include property details in the output.
36+
/// Specifies whether to include the log's parameters dictionary in log messages.
2537
/// </summary>
38+
///
39+
/// <value>
40+
/// A boolean representing the configuration. If true, the log's parameters dictionary is included in the log message as a JSON. This setting is only applicable when TelegramSinkConfiguration.Mode is LoggingMode.Logs.
41+
/// </value>
2642
public bool IncludeProperties { get; init; }
43+
44+
/// <summary>
45+
/// Sets or gets the time zone used by this sink for log timestamps.
46+
/// </summary>
47+
///
48+
/// <value>
49+
/// The time zone information. If null, server time will be used.
50+
/// </value>
51+
///
52+
/// <remarks>
53+
/// This property is specifically for this logger sink. It does not affect other sinks utilized by Serilog.
54+
/// Please note that each sink might handle log timestamps based on its individual configuration. To ensure the desired time zone is applied to your logs, you need to set this property for each necessary sink.
55+
/// </remarks>
56+
public TimeZoneInfo? TimeZone { get; init; }
2757
}

src/X.Serilog.Sinks.Telegram/Formatters/DefaultAggregatedNotificationsFormatter.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ public override List<string> Format(
1515

1616
private List<string> DefaultFormatter(IEnumerable<LogEntry> logEntries, FormatterConfiguration config)
1717
{
18-
var entries = logEntries as LogEntry[] ?? logEntries.ToArray();
19-
2018
var sb = new StringBuilder();
21-
logEntries = entries.OrderBy(x => x.UtcTimeStamp);
2219

23-
var beginTs = logEntries.First().UtcTimeStamp;
24-
var endTs = logEntries.Last().UtcTimeStamp;
20+
logEntries = logEntries.OrderBy(x => x.UtcTimeStamp).ToList();
21+
22+
var batchBeginTimestamp = logEntries.First().UtcTimeStamp;
23+
var batchEndTimestamp = logEntries.Last().UtcTimeStamp;
24+
25+
if (config.TimeZone is not null)
26+
{
27+
batchBeginTimestamp = TimeZoneInfo.ConvertTime(batchBeginTimestamp, config.TimeZone);
28+
batchEndTimestamp = TimeZoneInfo.ConvertTime(batchBeginTimestamp, config.TimeZone);
29+
}
2530

26-
sb.Append("<em>[").Append($"{beginTs:G}").Append('—').Append($"{endTs:G}").Append("]</em>").Append(' ')
31+
sb.Append("<em>[").Append($"{batchBeginTimestamp:G}").Append('—').Append($"{batchEndTimestamp:G}")
32+
.Append("]</em>").Append(' ')
2733
.Append(config.ReadableApplicationName)
2834
.AppendLine()
2935
.AppendLine();

src/X.Serilog.Sinks.Telegram/Formatters/DefaultLogFormatter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ private string FormatMessageInternal(LogEntry logEntry, FormatterConfiguration c
5454

5555
var sb = new StringBuilder();
5656

57-
sb.Append(level).Append(' ').Append("<em>[").Append($"{logEntry.UtcTimeStamp:G}").Append("]</em>").Append(' ')
57+
var timestamp = logEntry.UtcTimeStamp;
58+
if (config.TimeZone is not null)
59+
{
60+
timestamp = TimeZoneInfo.ConvertTime(timestamp, config.TimeZone);
61+
}
62+
63+
sb.Append(level).Append(' ').Append("<em>[").Append($"{timestamp:G}").Append("]</em>").Append(' ')
5864
.Append(config.ReadableApplicationName);
5965

6066
sb.AppendLine();

0 commit comments

Comments
 (0)