In .NET, there is built-in API that supports logging (Microsoft.Extensions.Logging) and there are many third-party logging providers that implement that API.
For simple use cases, the built-in logging can be sufficient, but for many or our projects at Trailhead, we need a more powerful and flexible logging system. For these projects we often use a third-party provider, and Serilog is often the one we find best for our needs.
Getting Started with Serilog
There are couple NuGet libraries needed to setup Serilog in your ASP.NET Core project:
Once installed and configured, you can write log messages directly through Serilog or any ILogger interface injected by ASP.NET. All loggers will use the same underlying implementation, levels, and destinations. This was ideal for us because we had existing project, with written logs by built-in provider – Microsoft.Extensions.Logging so all we needed to do is to add Serilog configuration.
Configuring Serilog
With Serilog.Settings.Configuration installed, the Serilog settings provider reads from any Microsoft.Extensions.Configuration sources including the appsettings.json file. We added these configurations in our project since this is all we need for now.
Serilog has all kinds of “sinks” that it can use for writing logged events to different types of storage in different formats. For example, you can use sinks to log to Azure or AWS.
If it’s sufficient for your needs, you can also simply log to files. As you can see in configuration code above, there is WriteTo section where you add either “Console” (for Serilog.Sinks.Console when running in development) or ”File” (for Serilog.Sinks.File when running in productio). You can add all kinds of parameters for the File sink. In the example above, I’ve added path and rollingInterval, which can be either Day, Hour, Month, Year, etc. Finally, I used an outputTemplate, which formats the contents of each event log entry.
The last thing I need to do is configure Serilog using this configuration section of the appsettings.json file in Program.cs:
As you can see, configuration is quite simple. There is LoggerConfiguration instance where you should either pull configuration from appsettings.json or hard-code it directly in the code. Here’s an example of that:
It is always better practice to use it from appsetings.json so it can be configurable and override-able in different environments. For example, ASP.NET Core allows you to use a different configuration file on different environments.
When using environment-based configuration files, you could set different environments up differently. For example, you could configure appsetings.Development.json to log to both Console and File, while for appsettings.Production.json you can add also to log to Azure (since you may not want to increase your storage on Azure while doing development).
Conclusion
That’s a quick overview of how to use Serilog in an existing ASP.NET Core project. I hope this will save you some time if you’ve been investigating how to do that and which logging framework to use.