Logging in .NET Framework applications using Serilog
Published: 2024-03-03 18:42:53

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](https://github.com/serilog/serilog/wiki/Provided-Sinks). This feature allows developers to log in different destinations, such as console, file, log servers, etc. Also, Serilog provides [enrichments](https://github.com/serilog/serilog/wiki/Enrichment), 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
1. Create an ASP.NET Web Application (.NET Framework)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndg89rh0jx8lc8lijbl8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9u91kvf6wgk746lvha4.png)
2. Install the following packages:
```c#
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 a `Serilog` structured logging in the `Web.config` file.
```xml
<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` in `appSettings` section in the `Web.config` file

```xml
<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.

```c#
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 the `Application_Start` event be raised.

### Time to log

```c#
ILog logger = LogManager.GetLogger<MvcApplication>();
logger.Info("This is my first log");
```

### Summary
You can downland and review the example [here](https://github.com/MichaelHorta/WebApplication.That.Works.With.Serilog)