Creating a Serilog Custom Enricher
Tags: csharp, dotnet, dotnetcore
Published: 2023-04-14 15:41:22

 **Serilog** is a popular logging library for **.NET** that provides a flexible and extensible way to log messages from applications. One of the key features of Serilog is the ability to **enrich log events** with additional contextual information that can help in troubleshooting and debugging.

In this blog post, we will explore how to create a custom enricher with Serilog to add custom properties to log events.

## 📢Development

To create a custom enricher with **Serilog** , we need to define a class that implements the `ILogEventEnricher` interface.

```csharp
public interface ILogEventEnricher
{
    void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory);
}
```

**ILogEventEnricher.cs Interface**

This interface contains a single method `Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)` that we need to implement to add custom properties to log events.

Let's start by creating a new class called `AspNetCoreEnvironmentEnricher` that implements the `ILogEventEnricher` interface:

```csharp
using Serilog.Core;
using Serilog.Events;
using System;

namespace Serilog.Labs.CustomEnrichers.Enrichers
{
    //class must implement interface ILogEventEnricher
    public class AspNetCoreEnvironmentEnricher : ILogEventEnricher
    {
        static readonly string _aspNetEnvironment;

        /// <summary>
        /// static constructor to load the environment variable
        /// </summary>
        static AspNetCoreEnvironmentEnricher()
        {
            _aspNetEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        }

        public void Enrich(
        	LogEvent logEvent, 
            ILogEventPropertyFactory propertyFactory)
        {
            var enrichProperty = propertyFactory
            	.CreateProperty(
                	"AspNetCoreEnvironment", 
                    _aspNetEnvironment);

            logEvent.AddOrUpdateProperty(enrichProperty);
        }
    }
}

```

**AspNetCoreEnvironmentEnricher.cs implementation**

The `Enrich` method is called for each log event, and we can use it to add custom properties to the `logEvent` parameter. The `propertyFactory` parameter can be used to create new properties that we can add to the log event.

### Using AspNetCoreEnvironmentEnricher

Now that we have our custom enricher class, let's use it with Serilog. We can add it to the logger configuration like this:

```csharp
Log.Logger = new LoggerConfiguration()
    .Enrich.With(new AspNetCoreEnvironmentEnricher())
    .WriteTo.Console(
    	outputTemplate: "[{AspNetCoreEnvironment} {Level:u3}] {Message:lj}{NewLine}{Exception}")
    .CreateLogger();

```

**Using the AspNetCoreEnvironmentEnricher**

In this example, we added our custom enricher to the logger configuration using the `Enrich.With` method. We passed the current user's name "Alice" to the constructor of our enricher.

Now, when we log a message, the `UserName` property will be added to the log event:

```csharp
Log.Information("Hello, world!");

/*Will Output
[Information Development] Hello, world!
*/
```

## 😎Conclusion

Creating a custom enricher with Serilog is a simple and powerful way to add contextual information to log events.

By implementing the `ILogEventEnricher` interface and using the `propertyFactory` parameter, we can add any custom properties we need to help in troubleshooting and debugging our applications.