serilog / serilog-sinks-email

A Serilog sink that writes events to SMTP email
Apache License 2.0
73 stars 68 forks source link

Send different errors to different recipients #84

Closed fairking closed 4 years ago

fairking commented 4 years ago

I am looking for a logger which can allow me to send different errors to different recipients. For example the ConnectionException or any derived exceptions must go to the service@mail.com email but any other Exception must be sent to the devs@mail.com address. Is it possible with serilog?

fairking commented 4 years ago

Could be also an ability to look for an InnerException which also can be ConnectionException.

fairking commented 4 years ago

Also those different recipients could receive emails in different formats. Eg. the ConnectionException can contain a message only without a stacktrace.

AR1ES commented 4 years ago

@fairking you can add multiple loggers with different filters.

var emailConnection1 = new EmailConnectionInfo {
    ...
    ToEmail = "service@mail.com"
};
var emailLog1 = new LoggerConfiguration().MinimumLevel.Error()
    .Filter.ByIncludingOnly(m=>m.Exception is ConnectionException || m.Exception.InnerException is ConnectionException ... )
    .WriteTo.Email(emailConnection1, restrictedToMinimumLevel: LogEventLevel.Error,outputTemplate: "...."
).CreateLogger();

var emailConnection2 = new EmailConnectionInfo {
    ...
    ToEmail = "dev@mail.com"
};
var emailLog2 = new LoggerConfiguration().MinimumLevel.Error()
    .Filter.ByExcluding(m=>m.Exception is ConnectionException || m.Exception.InnerException is ConnectionException ... )
    .WriteTo.Email(emailConnection2, restrictedToMinimumLevel: LogEventLevel.Error,outputTemplate: "...."
).CreateLogger();

logConfig = logConfig.WriteTo.Logger(emailLog1).WriteTo.Logger(emailLog2);
var log = logConfig.CreateLogger();