Using SeriLog in ASP.NET Core MVC 6 – Windows ASP.NET Hosting 2016 Best | Review and Comparison
 

When thinking about deploying your application to a live server you will more than likely want logging functionality. It’s the only real way of troubleshooting any issues that may arise.

ASP.NET Core MVC 6 makes it really easy to add logging to the console or debug window in your Startup.cs file.

In ConfigureServices method simply add:

services.AddLogging();

And then inside Configure method add:

if (env.IsDevelopment())
 {
 loggerFactory.AddDebug(LogLevel.Information);
 }
 else
 {
 loggerFactory.AddDebug(LogLevel.Error);}
with the loggerFactory passed into the method as:
public void Configure(...ILoggerFactory loggerFactory...)
 {

The check to see if you are running in development or not is optional but recommended.

Okay but in production we are going to need more than writing out to console.

The simplest solution is to write your log to a text file. In order to do this we will add the Serilog Nuget Package. Serilog is the only logging package capable of running in ASP.NET 5 at the time of writing (early 2016), and even then you will need to add the pre-release version and remove the dotnetcore runtime. I’m sure by late 2016 you will not need to run pre-release and maybe not even have to remove the dotnetcore runtime, check the Serilog site for details on this.

Open up your package manager window and enter:

Install-Package Serilog -Pre

you will need to add 3 more Serilog packages in order to work with Rolling Log files:

Install-Package Serilog.Sinks.RollingFile -pre
Install-Package Serilog.Sinks.RollingFile.Extension -pre
Install-Package Serilog.Extensions.Logging -pre

If any of these installs don’t work simply search for them on the Nuget website to see if instructions have changed.

As mentioned, at the time of writing the .Net Core dnx runtime will not build with this Serilog library added to your project. So if you have to have this runtime then you should surround your Serilog code with the #if DNX451 #endif tags so it will only run with the regular .Net Framework. alternatively if you don’t need the .Net Core (and many people wont) then simply remove it from the project.json file altogether – you should be left with this:

"frameworks": {
 "dnx451": {
 }
 },

I would expect when ASP.NET Core is fully released (it’s only at RC1 at time of writing then these Core CLR issues will be fixed).

Rolling Files?

Rolling files are essentially just text files with some added, very useful, functions. Serilog Rolling files have a max file size limit by default (1gb) which can be changed and a max number of files to create before old files are deleted (31).

Okay so let’s add our logger to Startup.cs class. Inside the Startup Constructor:

#if DEBUG
 Log.Logger = new LoggerConfiguration()
 .MinimumLevel.Debug()
 .WriteTo.RollingFile("../logs/log-{Date}.txt").CreateLogger();
#else
 Log.Logger = new LoggerConfiguration()
 .MinimumLevel.Information()
 .WriteTo.RollingFile("../logs/log-{Date}.txt").CreateLogger();
#endif

The reason we used an ‘if else’ is because of how Visual Studio publishes Web Apps – It auto creates a ‘logs’ folder at the root level alongside the wwwroot and approot folders so you might as well use this folder as the home for our Production log files. Using this ‘if else’ we also have the option to not log to file at all in Debug mode or log to a different location, and of course log a different level of information.

The only other thing we need to do is add .Serilog() to our already existing loggerFactory:

if (env.IsDevelopment())
 {
 loggerFactory.AddDebug(LogLevel.Information).AddSerilog();
 }
 else
 {
 loggerFactory.AddDebug(LogLevel.Error).AddSerilog();}

Using the Logger

To use our new logger simply add the logger to your class constructor (controller, context, etc), e.g.:

public HomeController(...ILogger<HomeController> logger)

Then, inside your various methods:

Try
 {
 _logger.LogInformation("Start - foo....");
...
 }
 catch(Exception ex)
 {
 _logger.LogError("End - foobar", ex);
...
 }