Click here to Skip to main content
15,741,047 members
Articles / Web Development / ASP.NET
Posted 20 Oct 2015


54 bookmarked

Serilog - An Excellent Logging Framework Integrated With .NET Applications

Rate me:
Please Sign up or sign in to vote.
4.89/5 (16 votes)
20 Oct 2015CPOL5 min read
Serilog is a portable and structured logging framework to record diagnostic logs into files, console and SQL/NoSQL databases.


Logging is an approach to record information about a program's execution for debugging and tracing purposes. Logging usually involves writing text messages to log files or sending data to monitoring applications. The logged information gives you a good understanding about processed data, warnings, errors and more. In practice, logging enables developers and testers to identify software problems, monitor live systems and verify outputs.

In .NET platform, there are multiple matured logging frameworks such as:

They are designed for simplifying developers' work regarding processing logs. All the logging frameworks work basically the same: you include one of the libraries into your programs and add logging statements to your code. If you start your application and logging is enabled, logging information is sent to the configured logging destination like a log file, console or database.

Serilog is an excellent logging framework and has been active for years. Unlike other logging libraries for .NET, Serilog is built with structured log data in mind. This framework is built around the idea that log messages should be more than a collection of strings. Log messages are preserved as structured data(JSON) that can be written in document form to a variety of output providers.

Structure of serilog

Image 1


  • Simple API and easy to extend
  • Structured data to preserve your data’s semantics
  • Log to any a variety of targets


Before we get started, let's have a quick overview at how easy it is to apply Serilog into your project. Please see the below examples:

  1. Basic logger:
    using Serilog;
    namespace SerilogTutorial
        public class HelloSerilog
            public static void Main(string[] args)
                // Create Logger
                var logger = new LoggerConfiguration()
                // Output logs
                logger.Information("Hello, Serilog!");

    This example writes a line like the following to Console.

    2015-10-19 15:39:06 [Information] Hello, Serilog!
  2. Pass parameters to loggers:
    // Create Logger
    var logger = new LoggerConfiguration()
    // write structured data
    logger.Information("Processed {Number} records in {Time} ms", 500, 120);

    It writes a line as follows:

    2015-10-19 22:24:09 [Information] Processed 500 records in 120 ms
  3. Create Template loggers:
    // Create Logger
    var logger = new LoggerConfiguration()
    // prepare data
    var order = new {Id = 12, Total = 128.50, CustomerId = 72};
    var customer = new {Id = 72, Name = "John Smith"};
    // write log message
    logger.Information("New orders {OrderId} by {Customer}", order.Id, customer);

    It prints logs as follows:

    2015-10-19 22:32:43 [Information] New orders 12 by "{ Id = 72, Name = John Smith }"

As we can see, the power of Serilog is it supports process message parameters and structured data. While traditional logging libraries render messages into strings, Serilog defers rendering and captures the parameters separately from the message template.

Using the Code

Now let's go deep into how to apply Serilog into your projects. Here are the basic steps:

  • Preparation
  • Install serilog
  • Create a logger
    • Configure logger
    • Add sinks
  • Write log messages
  • Customize logger
    • Define log output template
    • Specify minimal level
    • Install database sink


Of course, you need to have Visual Studio installed and have a project ready. For simplify purpose, you just need to create a console application.

Install serilog

Typically, there are two ways to install nuget packages.

Way 1 - via Nuget Package Manager

Right click your project > Manage nuget packages > search serilog

Image 2

Way 2 - via Project Manager Console

Goto Menu > Tools > NuGet PackageManager > Project Manager Console
PM> Install-Package Serilog

Image 3

Create a Logger

Logger is the fundamental of writing logs and it is used to output log messages. To create a logger, you need to specify two settings.

Configure Logger

You need to create a LoggerConfiguration object

Add Sinks

Sink is a place or destination to process logs. Sink could be console, file or data store. Sink is configured using WriteTo() method.

Now, you can build a logger to write events to Console:

var logger = = new LoggerConfiguration()

Multiple additional sinks can be added through chaining WriteTo methods:

var logger = new LoggerConfiguration()

Write Log Messages

Now the logger is ready to use. You can simply write logs as follows:

log.Debug("Debugging message");
log.Information("Information message");
log.Warning("Warning message");
log.Error("Error message");
log.Fatal("Fatal message");

Customize Logger

In order to modify logger to fit your needs, you need to tune the logger.

Define Log Output Template

Log message template controls how the sink renders events. Message templates are a superset of standard .NET format strings and compatible with string.Format() method. Moreover, it supports JSON objects or regular C# objects(with @ symbol) as parameters.

var position = new { Latitude = 25, Longitude = 134 };
var elapsedMs = 34;
log.Information("Processed {@Position} in {Elapsed} ms.", position, elapsedMs);

It prints logs like the following:

09:14:22 [Information] Processed { Latitude: 25, Longitude: 134 } in 34 ms.

Specify Minimal Level

There are five levels of logs:

Level Usage
Verbose Tracing information and debugging minutiae; generally only switched on in unusual situations
Debug Internal control flow and diagnostic state dumps to facilitate pinpointing of recognized problems
Information Events of interest or that have relevance to outside observers; the default enabled minimum logging level
Warning Indicators of possible issues or service/functionality degradation
Error Indicating a failure within the application or connected system
Fatal Critical errors causing complete failure of the application

Serilog writes the logs with minimal level as well as higher and ignore lower levels. For instance, specify minimal level as Debug will output logs except Verbose.

var logger = new LoggerConfiguration()

You can also override minimal level. For instance, we want to display all logs higher than Debug in the console but save all error logs to databases. In order to meet this requirement, you can specify global level as Debug and set database sink with Warning level.

var logger = new LoggerConfiguration()
    .WriteTo.MongoDB("mongo://myserver/logs", minimumLevel: LogEventLevel.Warning)
logger.Information("I am an information log"); // will print in console
log.Error("I am an error log");// will print in console and also store in database

Write error-level logs events to File:

var logger = new LoggerConfiguration()
    .WriteTo.File("log.txt", LogEventLevel.Error)
logger.Information("I am an information log"); // will not in log file
logger.Error("I am an error log"); // will write to log file

If no MinimumLevel is specified, Serilog will use Information level events as default - Information and higher levels will be processed. 

Install Database Sink

Serilog supports build-in sinks include Console, File (single/rolling) and TextWriter. In order to enable serilog export logs to different destinations, additional sinks need to be installed.

A typical case is to write logs to database, you need to install additional sinks. For instance, using SQL Server database as destination needs to install SQL Server sink.

PM> Install-Package Serilog.Sinks.MSSqlServer

Create database LogEvents and table Logs using SQL script:

if not exists(select * from sys.databases where name='LogEvents')
         create database LogEvents;
Use LogEvents;
         [Id] [int] IDENTITY(1,1) NOT NULL,
         [Message] [nvarchar](max) NULL,
         [MessageTemplate] [nvarchar](max) NULL,
         [Level] [nvarchar](128) NULL,
         [TimeStamp] [datetime] NOT NULL,
         [Exception] [nvarchar](max) NULL,
         [Properties] [xml] NULL,
         [Id] ASC

The script will create LogEvents database and Logs table:

Image 4

Write to SQL Server database sink:

var logger = new LoggerConfiguration()
        	Trusted_Connection=True;", "Logs")
logger.Information("I am an information log");
logger.Error("Hello, I am an error log");

You will get:

Image 5


This article only covers essential conceptions of Serilog. More advanced features can be available here. Advance topics include:

  • Add dynamic properties through Enrichers
  • Add log filters to filter out noisy events and select particular logs
  • Add sub loggers to process complex logging requirements
  • More sinks such as MongoDB, Seq, RavenDB, Email and Azure DocumentDB. The complete list can be found here.


  • October 20, 2015: First version posted
  • October 20, 2015: Fix format


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Canada Canada
Andy Feng is a software analyst/developer based in Toronto, Canada. He has 9+ years experience in software design and development. He specializes in Java/J2EE and .Net solutions, focusing on Spring, Hibernate, JavaFX, ASP.NET MVC, Entity framework, Web services, JQuery, SQL and related technologies.

Follow up with my blogs at:

Comments and Discussions

QuestionSerilog logging to a file Pin
Member 1469722022-Dec-19 6:18
Member 1469722022-Dec-19 6:18 
PraiseVery Helpful Pin
saqibamin25731-Jul-19 2:27
saqibamin25731-Jul-19 2:27 
QuestionThread safety question Pin
peterkm20-Oct-15 23:26
peterkm20-Oct-15 23:26 
AnswerRe: Thread safety question Pin
Andy Feng21-Oct-15 3:51
Andy Feng21-Oct-15 3:51 
GeneralRe: Thread safety question Pin
peterkm22-Oct-15 4:47
peterkm22-Oct-15 4:47 
GeneralRe: Thread safety question Pin
kiquenet.com10-Dec-18 0:47
professionalkiquenet.com10-Dec-18 0:47 
Generalit might... Pin
John Torjo20-Oct-15 9:43
professionalJohn Torjo20-Oct-15 9:43 
GeneralRe: it might... Pin
Andy Feng20-Oct-15 10:13
Andy Feng20-Oct-15 10:13 
Sorry for the confusing words. Actually, it will print the same message except the datetime maybe different - depends on when you run it.

GeneralRe: it might... Pin
John Torjo20-Oct-15 10:40
professionalJohn Torjo20-Oct-15 10:40 
GeneralRe: it might... Pin
Andy Feng20-Oct-15 11:30
Andy Feng20-Oct-15 11:30 
GeneralRe: it might... Pin
John Torjo20-Oct-15 23:27
professionalJohn Torjo20-Oct-15 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.