The purpose of this article is to provide a step-by-step guide about how configure Serilog library in a .NET Framework 4.6.2 (or above) application.
What is Serilog?
Serilog is a diagnostic logging library for .NET applications. This library provides a huge collection of new logging related features that are not available in the .NET built-in logging framework. Serilog uses what are called sinks. This feature allows developers to log in different destinations, such as console, file, log servers, etc. Also, Serilog provides enrichments, a way to inject data to enrich logs.
In this article we'll work with both, sinks and enrichments, to provide an example as complete as posible.
Setup
- Create an ASP.NET Web Application (.NET Framework)
- Install the following packages:
Common.Logging/3.4.1 // introduces a simple abstraction to allow you to select a specific logging implementation at runtime
Common.Logging.Serilog/4.4.0 // allows to adapt Common.Logging to Serilog
Serilog/3.1.1
Serilog.Enrichers.Environment/2.3.0 // provides information from the environment, such as machine name, environment name, etc.
Serilog.Enrichers.Thread/3.1.0 //provides properties from the current thread
Serilog.Settings.AppSettings/2.2.2 // allows to read serilog settings written in the appSettings section from the app.config/web.config file.
Serilog.Sinks.File/5.0.0 // allows to log into the file system.
Configure logging with Serilog
library
- Configure the adapter from the old
Common.Logging
to aSerilog
structured logging in theWeb.config
file.
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Serilog.SerilogFactoryAdapter, Common.Logging.Serilog" />
</logging>
</common>
- Configure
Serilog
inappSettings
section in theWeb.config
file
<add key="serilog:minimum-level" value="Information" />
<add key="serilog:using:Thread" value="Serilog.Enrichers.Thread" />
<add key="serilog:enrich:WithThreadId" />
<add key="serilog:using:Environment" value=" Serilog.Enrichers.Environment" />
<add key="serilog:enrich:WithMachineName" />
<add key="serilog:enrich:WithEnvironmentName" />
<add key="serilog:using:File" value="Serilog.Sinks.File" />
<add key="serilog:write-to:File.path" value="C:\logs\WebApplication.That_Works_With_Serilog..log" />
<add key="serilog:write-to:File.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}" />
Build global logger for application
- In
Global.asax.cs
file write the below method.
private void BuildLogging()
{
LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
loggerConfiguration.ReadFrom.AppSettings();
Serilog.Core.Logger logger = loggerConfiguration.CreateLogger();
Log.Logger = logger;
}
- Now, you must invoke
BuildLogging
method when theApplication_Start
event be raised.
Time to log
ILog logger = LogManager.GetLogger<MvcApplication>();
logger.Info("This is my first log");
Summary
You can downland and review the example here
Top comments (0)