In this interactive guide, we will walk you through the process of setting up Serilog Email Sink in a .NET Core application to send log emails.
By the end of this guide, you’ll have a working implementation that sends log events to your specified email address. Let’s get started!
Use Case for Serilog Email Sinks
- Error Monitoring: When critical errors occur in your application, it’s essential to be notified immediately. By configuring the Serilog email sink, you can receive email notifications whenever an error or exception is logged. This allows you to respond quickly to issues and take appropriate actions.
- Alerting and Notifications: Besides errors, you may want to receive notifications for specific events or conditions in your application. By logging important events with a severity level that triggers an email, you can use the email sink as an alerting mechanism. For example, you can send emails for security breaches, performance bottlenecks, or unusual user activities.
Setting up the Project
Let’s utilize our Dk.Serilog.Demo GitHub project to demonstrate the email sinks with Serilog. You can download the existing project from this Github Link or You need to just set up the .Net Core App using your favorite IDE.
Once the initial setup is done. We need to install the following Nuget packages.
- Serilog – Install this if you are setting up a fresh new project.
- Serilog.Sinks.Email, Version 2.0.0
Once the installation is done. We need to append the configuration section to capture the Email related settings like username, password, and from email.
{ "Serilog": { "MinimumLevel": { "Default": "Information", "Override": { "DK.Serilog.Demo.Services": "Warning", "Microsoft.AspnNetCore": "Warning" } }, "WriteTo": [ { "Name": "Console", "Args": { //"formatter": "DK.Serilog.Demo.Infrastructure.CustomFormatter, DK.Serilog.Demo" } }, { "Name": "Email", "Args": { "ConnectionInfo": { "FromEmail": "[email protected]", "ToEmail": "[email protected]", "Port": "25", "MailServer": "localhost", "EnableSsl": false, "EmailSubject": "Exception in Dk.Serilog.Demo Application" }, "restrictedToMinimumLevel": "Warning", "batchPostingLimit": 100 } ], "DurableTask": { "BatchSizeLimit": 1000, "QueueLimit": 10000, "Period": "00:00:02", "BackgroundQueueSize": 1000 } } }
Most of the configurations you might have seen earlier as well in case of any email-sending setup. However, there are two config properties important here.
restrictedToMinimumLevel
: Specifies the minimum logging level for the messages to be sent via email. Only log messages with a severity level equal to or higher than this value will be included in the email. In this case, it is set to “Warning”, meaning that only warning-level and higher messages will be included.batchPostingLimit
: Specifies the maximum number of log messages to include in a single email. In this example, it is set to 10, meaning that each email will contain only one log message.
Now the setup is done, lets run the application and wait for the email in the mailbox. I have used PaperCut Local SMTP Server for testing.
And, now lets open the PaperCut Server UI. Where we can find the emails which has been sent by the Serilog.
Conclusion
Serilog email sinks offer a convenient way to send log messages via email, enabling various use cases such as error monitoring, alerting, debugging, log aggregation, compliance, and audit trails.
By configuring the email sink, you can receive timely notifications for critical errors, important events, or specific conditions in your application.
It can assist in troubleshooting, centralize log data, and maintain an auditable trail of log messages. However, for larger-scale applications or real-time monitoring, dedicated log management systems or services may be more suitable.