Structured Logging With Serilog and Seq: Part 2

Introduction

 
This is the second part of my article series for structured logging with Serilog. Part one of this series is Structured Logging with Serilog and Seq - Part 1. Now, I will introduce the most common and simple Sink used Seq. Seq can be downloaded from here.
 
The following figure describes the seq functionality.
 
 
First, write data on the seq server using the HTTP calls. Using the queries you can get logged data. As said above, seq can be downloaded from here.
 
After downloading, run the seq installation. During the installation, you will find the following screen.
 
The default installation of the seq file is "C:\Program Files\Seq", and ou can access the seq from http://localhost:5341 (port 5341 is the default port for the seq).
 
 
 
I have already created the serilog application in Part 1. Now, I am introducing the code for writing the log in seq.
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Reflection;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Serilog;  
  9. using Serilog.Core;  
  10. using Serilog.Events;  
  11.    
  12. namespace SeriLogIntro2  
  13. {  
  14.     class Program  
  15.     {  
  16.         static void Main(string[] args)  
  17.         {  
  18.             ILogger logger = new LoggerConfiguration()  
  19.                    .Enrich.With(new ApplicationDetailsEnricher())  
  20.                    .WriteTo.Seq("http://localhost:5341")  
  21.                    .MinimumLevel.Verbose()  
  22.                             .CreateLogger();  
  23.    
  24.          logger.Information("User name is {username} and age is {age}""vivek", 34);   
  25.             Log.Logger = logger;  
  26.             logger.Information("fav shape is {@shape}", favshape1);   
  27.             Console.ReadLine();  
  28.         }  
  29.     }       
  30. }  
In the preceding code, you can see the highlighted line, this is the way log is written on the seq server. When running this code, you will see the following log in the seq (you can get the seq from http://localhost:5341).
 
 
In this screenshot, you will see that, except for the information we logged, there are two more properties (ApplicationName and ApplicationVersion). This is a very useful feature that is called Enrichment.
 
Enrichment: This is the feature by which properties are automatically added to the log messages, as you will see in the sample code.
  1. public class ApplicationDetailsEnricher : ILogEventEnricher  
  2.     {  
  3.       public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)  
  4.         {  
  5.             var applicationAssembly = Assembly.GetEntryAssembly();  
  6.    
  7.             var name = applicationAssembly.GetName().Name;  
  8.             var version = applicationAssembly.GetName().Version;  
  9.    
  10.             logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("ApplicationName", name));  
  11.             logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("ApplicationVersion", version));  
  12.         }  
  13.     }  
As shown above, the class is inherited by the ILogEventricher Interface and the enrich method takes two arguments, LogEvent and ILogEventPropertyFactory. Using this, we create a property to log.
 
In the seq, you can create queries/views for getting some generic type of log information as seen in the following example.
 
 
 
 
So, you can see, we can create a different view and query based on our requirements (please see the following screenshots).
 
 
There are many more options available on the settings tab, such as retention policy, so that we can add a policy to retain the messages in the log.
 
 
So, using the serilog/seq implementation you can maintain your log in a very structured way in a no-SQL form. You can get the log very easily using simple queries over HTTP(s).


Recommended Free Ebook
Similar Articles