asp .net core logging with Serilog

Default Logging

By default ASP. NET Core provide logging mechanism from Microsoft.Extension.Logging. This logging provide log information into console that displays while application is running. The configuration is simple and straight forward within json configuration file, this file is where setting for LogLevel which determines the level of logging information that is captured and displayed. The default configuration and Startup file is bellow.

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace serilog_mvc_demo
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

The logging above is sufficient, however there is not a log file that is created since this logging only outputs to console interface.

Microsoft Logging in Console

Serilog ASP .NET Core

The advantage of using Serilog instead of default logging is that we can provide logging output into console, file and other. In my opinion this solution is way better than the default. In order to add serilog into our project, is to add Serilog.AspNetCore package and modify Program.cs and add serilog configuration into appsettings.json.

{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "log/log.txt",
//"outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 20
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName"
],
"Properties": {
"Application": "AIMS"
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
namespace serilog_mvc_demo
{
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((hosting, logerconfig)=>
logerconfig.ReadFrom.Configuration(hosting.Configuration))
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
view raw Program.cs hosted with ❤ by GitHub

As shown above in appsettings.json, serilog writes to console and file. The file outputs is located in log folder with name format log.txt. The configuration also specify the rolling interval which sets to Day and filecountlimit to 20, this means the log file will rotate each day and only 20 files that is kept in the folder. The older log file will be deleted automatically, makes it easier to maintain the application since we don’t have to worry about running out of disk space because we forget delete older log file.

The picture bellow shows serilog outputs to console and file, notice the output folder and file name that is generated according to our configuration above.

Serilog Console Output
Serilog File Output

Published by Gadael Sedubun

Developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: