Add Extra Field To Serilog For Effective Log Analysis And Debugging In Asp.Net Core



Published On Friday June 19, 2020
Reading Time: 2 minutes

On the previous blog, we added serilog for our .net core application. But sometimes we may require to add some custom fields to be more precise for our application. Since I have worked on the fintech industry, during our log monitoring we used to assign a unique id for a specific transaction. This is done so as to debug the transaction efficiently.  Before moving forward, If you have not viewed the previous post you can visit it Implementing Serilog With Seq For Log Monitoring.

So we will start with the appsettings.json file.

"Serilog": {
   "WriteTo": [
     {
       "Name": "Seq",
       "Args": {
         "serverUrl": "http://localhost:5341", //url of seq application
         "apiKey": "" //api key used for seq application to connect too
       },
       "columnOptionsSection": {
         "customColumns": [
           {
             "ColumnName": "UniqueId",
             "DataType": "varchar",
             "DataLength": 150
           }
         ]
       }
     }
   ]
 }

Here we have added a section columnOptionsSection, and under it customColumns, the node is an array and we can add as much as fields we require here. For the demonstration, I have added a field with the name UniqueId, its data type, and data length. This is the only configuration we require.

Now, for logging the details, it can be achieved by using the ForContext method of serilog. Which is represented with the code snippets. For this, I have used the default controller of the scaffolded web API project in .net core i.e. WeatherForecastController.

public class WeatherForecastController : ControllerBase
{
    private readonly ILogger _log = Log.ForContext<WeatherForecastController>();//For Context Specifies The Class Which The Log Is Intended For

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            Guid guid = Guid.NewGuid(); //Used Guid for unique id generation
            _log.ForContext("UniqueId", guid).Information("Logging data with the unique Id"); //logging pattern with custom columns(fields)
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
}

Here the only change is the pattern, if you want to have the new field i.e. UniqueId to be the field you simply need to log the details with ForContext Method followed by the log level and description.

_log.ForContext("UniqueId", guid).Information("Logging data with the unique Id");

 

Now, the log in seq will be viewed as

Logging And Debugging, Add Extra Field To Serilog For Effective Log Analysis And Debugging In Asp.Net Core

The log can also be filtered using the search bar and query as

UniqueId = '93e27d6e-06e1-4e9b-aee3-4df9d2b189c5'
Logging And Debugging, Add Extra Field To Serilog For Effective Log Analysis And Debugging In Asp.Net Core

Did you find this blog post helpful? If so, whom could you share it with?

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x