Skip to content

Commit bc81d3b

Browse files
committed
Removed failurecallback option.
1 parent b3c6738 commit bc81d3b

File tree

3 files changed

+17
-58
lines changed

3 files changed

+17
-58
lines changed

src/Serilog.Sinks.Telegram.Alternative/LoggerConfigurationTelegramExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public static class LoggerConfigurationTelegramExtensions
3737
/// <param name="includeStackTrace">Whether stack traces should be included or not.</param>
3838
/// <param name="dateFormat">The date time format showing how the date and time should be formatted.</param>
3939
/// <param name="applicationName">The name of the application sending the events in case multiple apps write to the same channel.</param>
40-
/// <param name="failureCallback">The failure callback.</param>
4140
/// <param name="useCustomHtmlFormatting">A value indicating whether custom HTML formatting in the messages could be used. (Use this carefully and only if really needed).</param>
4241
/// <param name="botApiUrl">The Telegram bot API url, defaults to https://api.telegram.org/bot.</param>
4342
/// <param name="outputTemplate">A output template that can be used to format the output data.</param>
@@ -59,7 +58,6 @@ public static LoggerConfiguration Telegram(
5958
bool? includeStackTrace = true,
6059
string dateFormat = "dd.MM.yyyy HH:mm:sszzz",
6160
string applicationName = "",
62-
Action<Exception>? failureCallback = null,
6361
bool useCustomHtmlFormatting = false,
6462
string? botApiUrl = null,
6563
string? outputTemplate = null,
@@ -77,7 +75,6 @@ public static LoggerConfiguration Telegram(
7775
restrictedToMinimumLevel,
7876
sendBatchesAsSingleMessages,
7977
includeStackTrace,
80-
failureCallback,
8178
useCustomHtmlFormatting,
8279
botApiUrl,
8380
outputTemplate,

src/Serilog.Sinks.Telegram.Alternative/Sinks/Telegram/Alternative/TelegramSink.cs

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task EmitBatchAsync(IEnumerable<LogEvent> events)
9999
var message = this.options.FormatProvider is not null
100100
? extendedLogEvent.LogEvent.RenderMessage(this.options.FormatProvider)
101101
: this.outputTemplateRenderer is null ? RenderMessage(extendedLogEvent, this.options) : this.outputTemplateRenderer.Format(extendedLogEvent);
102-
await this.SendMessage(this.options.HttpClient, this.options.BotApiUrl, this.options.BotToken, this.options.ChatId, message, this.options.TopicId);
102+
await SendMessage(this.options.HttpClient, this.options.BotApiUrl, this.options.BotToken, this.options.ChatId, message, this.options.TopicId);
103103
}
104104
}
105105
else
@@ -127,7 +127,7 @@ public async Task EmitBatchAsync(IEnumerable<LogEvent> events)
127127
}
128128

129129
var messageToSend = sb.ToString();
130-
await this.SendMessage(this.options.HttpClient, this.options.BotApiUrl,this.options.BotToken, this.options.ChatId, messageToSend, this.options.TopicId);
130+
await SendMessage(this.options.HttpClient, this.options.BotApiUrl,this.options.BotToken, this.options.ChatId, messageToSend, this.options.TopicId);
131131
}
132132
}
133133

@@ -203,55 +203,29 @@ private static string RenderMessage(ExtendedLogEvent extLogEvent, TelegramSinkOp
203203
/// <param name="message">The message.</param>
204204
/// <param name="topicId">The Telegram topic id.</param>
205205
/// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
206-
private async Task SendMessage(HttpClient httpClient, string? botApiUrl, string token, string chatId, string message, int? topicId = null)
206+
private static async Task SendMessage(
207+
HttpClient httpClient,
208+
string? botApiUrl,
209+
string token,
210+
string chatId,
211+
string message,
212+
int? topicId = null)
207213
{
208-
this.TryWriteToSelflog($"Trying to send message to chatId '{chatId}': '{message}'.");
214+
SelfLog.WriteLine($"Trying to send message to chatId '{chatId}': '{message}'.");
209215

210216
var client = new TelegramClient(httpClient, token, botApiUrl);
211217

212-
try
218+
if (message.Length > MaxMessageLength)
213219
{
214-
if (message.Length > MaxMessageLength)
215-
{
216-
this.TryWriteToSelflog($"Message is too long to be sent, it must not exceed {MaxMessageLength} characters.");
217-
return;
218-
}
219-
220-
var response = await client.PostMessage(message, chatId, topicId);
221-
222-
if (response is not null)
223-
{
224-
this.TryWriteToSelflog($"Message sent to chatId '{chatId}': '{response.StatusCode}'.");
225-
}
226-
}
227-
catch (Exception ex)
228-
{
229-
#pragma warning disable CS0618 // Typ oder Element ist veraltet
230-
// Todo: Remove this in next version!
231-
this.options.FailureCallback?.Invoke(ex);
232-
#pragma warning restore CS0618 // Typ oder Element ist veraltet
233-
throw;
220+
SelfLog.WriteLine($"Message is too long to be sent, it must not exceed {MaxMessageLength} characters.");
221+
return;
234222
}
235-
}
236-
237223

238-
/// <summary>
239-
/// Tries to write to Selflog and throws an exception if a formatting error occurred.
240-
/// </summary>
241-
/// <param name="messageTemplate">The message template.</param>
242-
private void TryWriteToSelflog(string messageTemplate)
243-
{
244-
try
245-
{
246-
SelfLog.WriteLine(messageTemplate);
247-
}
248-
catch (Exception ex)
224+
var response = await client.PostMessage(message, chatId, topicId);
225+
226+
if (response is not null)
249227
{
250-
#pragma warning disable CS0618 // Typ oder Element ist veraltet
251-
// Todo: Remove this in next version!
252-
this.options.FailureCallback?.Invoke(ex);
253-
#pragma warning restore CS0618 // Typ oder Element ist veraltet
254-
throw;
228+
SelfLog.WriteLine($"Message sent to chatId '{chatId}': '{response.StatusCode}'.");
255229
}
256230
}
257231
}

src/Serilog.Sinks.Telegram.Alternative/Sinks/Telegram/Alternative/TelegramSinkOptions.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class TelegramSinkOptions
3939
/// <param name="minimumLogEventLevel">The minimum log event level to use.</param>
4040
/// <param name="sendBatchesAsSingleMessages">A value indicating whether the batches are sent as single messages or as one block of messages.</param>
4141
/// <param name="includeStackTrace">A value indicating whether the stack trace should be shown or not.</param>
42-
/// <param name="failureCallback">The failure callback.</param>
4342
/// <param name="useCustomHtmlFormatting">A value indicating whether custom HTML formatting in the messages could be used. (Use this carefully and only if really needed).</param>
4443
/// <param name="botApiUrl">The Telegram bot API url, defaults to https://api.telegram.org/bot.</param>
4544
/// <param name="outputTemplate">The output template.</param>
@@ -59,7 +58,6 @@ public TelegramSinkOptions(
5958
LogEventLevel minimumLogEventLevel = LogEventLevel.Verbose,
6059
bool? sendBatchesAsSingleMessages = true,
6160
bool? includeStackTrace = true,
62-
Action<Exception>? failureCallback = null,
6361
bool useCustomHtmlFormatting = false,
6462
string? botApiUrl = null,
6563
string? outputTemplate = null,
@@ -80,10 +78,6 @@ public TelegramSinkOptions(
8078
this.MinimumLogEventLevel = minimumLogEventLevel;
8179
this.SendBatchesAsSingleMessages = sendBatchesAsSingleMessages ?? true;
8280
this.IncludeStackTrace = includeStackTrace ?? true;
83-
#pragma warning disable CS0618 // Typ oder Element ist veraltet
84-
// Todo: Remove this in next version!
85-
this.FailureCallback = failureCallback;
86-
#pragma warning restore CS0618 // Typ oder Element ist veraltet
8781
this.DateFormat = dateFormat;
8882
this.ApplicationName = applicationName;
8983
this.UseCustomHtmlFormatting = useCustomHtmlFormatting;
@@ -152,12 +146,6 @@ public TelegramSinkOptions(
152146
/// </summary>
153147
public bool IncludeStackTrace { get; }
154148

155-
/// <summary>
156-
/// Gets the failure callback.
157-
/// </summary>
158-
[Obsolete("Use fallback logging instead. Check https://nblumhardt.com/2024/10/fallback-logging/.")]
159-
public Action<Exception>? FailureCallback { get; }
160-
161149
/// <summary>
162150
/// Gets a value indicating whether custom HTML formatting in the messages could be used. (Use this carefully and only if really needed).
163151
/// </summary>

0 commit comments

Comments
 (0)