Structured Logging With Serilog and Seq: Part 1

Introduction

In the current Software Development phase, logging is an integral part. The development process has been changed and in today's scenario, there are different API calls. So, structured logging is essential for debugging, troubleshooting or logging any other information. Serilog is useful for structured logging and therefore, the main purpose of using Serilog is to log the Structured Data.

Example for Logging

Have a look at the examples shown below. 
  1. Log.Information("the value of PI is {val}", Math.PI);
Where {val} is called a Named Property and Math.PI is the value of the named property.
  1. Log.Information("New user added to our system {UserName}, Age {UserAge}",name,age);  
Sinks: Sinks are the place where we store our log messages. For our demo application, we will use the sinks like a Console or File. There are many other sinks available that allow storing data in a structured format like Ravan DB/Seq.
Here's a list of some commonly used sinks.
  • Azure Table Storage
  • Console
  • File/Rolling file
  • Log4Net
  • Logger
  • MongoDB
  • Microsoft SQL Server
  • Raven DB
  • Seq

Logging Structured Data

  • Collection
  • Objects
  • Destructured Object

We will now create a sample application for Serilog.



Next, install the Nugget package for Serilog as shown below.





Code Sample for creating a console sink

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Serilog;  
  7. namespace SeriLogIntro2   
  8. {  
  9.     class Program   
  10.     {  
  11.         static void Main(string[] args)   
  12.         {  
  13.             ILogger logger = new LoggerConfiguration()  
  14.             //.WriteTo.RollingFile("rollingfile.txt")  
  15.             //.WriteTo.File("test.txt")  
  16.             .WriteTo.Console()  
  17.                 .CreateLogger();  
  18.             logger.Information("User name is {username} and age is {age}""vivek", 34);  
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22. }  
Now, have a look at the preceding example. Commented lines are for writing the logging in and rolling files.

From the following lines of code, you can understand how to assign the logger.
  1. Log.Logger = logger;  
  2. Log.Logger = logger;  
  3. IEnumerable favshape = new List < String > {  
  4.     "square""triangle"  
  5. };  
  6. Log.Information("fav shape is {shape}", favshape);  
  7. var favshape1 = new Shape() {  
  8.     square = 1,  
  9.     triangle = 2,  
  10.     rectangle = 3  
  11. };  
  12. logger.Information("fav shape is {@shape}", favshape1);  


Formatting

There are many formatting options available for Serilog.
  1. logger.Information("User name is {username} and age is {age}""vivek", 34);  
  2. logger.Information("User name is {username:l} and age is {age}""vivek", 34); //name without ""
The default Serilog shows a string in “”, but with the option {username:l}, we can remove “”.

Here's an example of numbers formatting.
  1. Log.Information("the value of PI is {val:0.00}", Math.PI);
Logging Levels
  • Verbose: Very low level debugging
  • Debug: low level control logic
  • Information
  • Warning
  • Error
  • Fatal

By default, the logging level is set to Information. We can set the minimum logging level in code.

  1. ILogger logger = new LoggerConfiguration()  
  2.     .WriteTo.Console()  
  3.     .MinimumLevel.Verbose()  
  4.     .CreateLogger();  
  5. Log.Verbose("Calculated {cvv} for {CardNumber}", check, card);  
  6. Log.Debug("Applying gold card for {Customer}", customer);  
  7. Log.Information("New {Card} placed by {Customer}", card, customer);  
  8. Log.Warning(exception, "Failed to save new {Order}, retrying in {Wait} milliseconds", order, retryDelay);  
  9. Log.Error("Failed to save new {file}", file);  
  10. Log.Error(exception, "Failed to save new {file}", file);  
  11. Log.Fatal("Unhandled exception, application is terminating");  
Adding the Log Context
  1. var contextLogger = Log.Logger.ForContext<Program>();   
I have introduced some basic understanding of Serilog in this article. There will be an introduction to some advanced features of Serilog and the Seq (Sinks) in my next article.


Similar Articles