Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog Email sink: single email at end of application

I am using Serilog.Sinks.Email in a console application written in C#. It is "restrictedToMinimumLevel" = "Warning", and I have Warning and Error messages sprinkled throughout the program.

When I ran this application yesterday, I received one email at the end of the run (when control reached Log.CloseAndFlush()) and this email had the consolidated warnings and errors generated during the run.

This is the desired functionality and I was delighted that I did not need to write a wrapper class that would implement it.

When I ran the same script today I received one email for every Error that was encountered.

I am puzzled by this erratic behavior. An extract from my App.config file is reproduced below:

<add key="serilog:using:Email" value="Serilog.Sinks.Email" />
<add key="serilog:write-to:Email.fromEmail" value="<email address>" />
<add key="serilog:write-to:Email.toEmail" value="<email address>" />
<add key="serilog:write-to:Email.mailServer" value="<mail server>" />
<add key="serilog:write-to:Email.mailSubject" value="<application name> [{MachineName}] [User: {UserName}]" />
<add key="serilog:write-to:Email.outputTemplate" value="{Timestamp:yyyy-MM-dd  HH:mm:ss}:: [{Level:u3}] {Message}{NewLine}" />
<add key="serilog:write-to:Email.restrictedToMinimumLevel" value="Warning" />

Is there any configuration setting that I should use in order to ensure that the script consistently sends at most one email (with the consolidated warnings and errors) at the end of any run?

Could anyone please point me to documentation that could help in this regard?

like image 454
Christna Avatar asked Jan 22 '26 03:01

Christna


1 Answers

I think you have reached the limit for one batch or have changed it. By default it is set to 100 as you can see in the code below from Serilog.Sinks.Email.EmailSink:

/// <summary>
/// A reasonable default for the number of events posted in
/// each batch.
/// </summary>
public const int DefaultBatchPostingLimit = 100;

Set this from your App.config to some limit which will not be reached for example:

<add key="serilog:write-to:Email.defaultBatchPostingLimit " value="999999" />
like image 78
Samvel Petrosov Avatar answered Jan 23 '26 17:01

Samvel Petrosov