Insights

Enrich your logging with Episerver data

Senior Developer
Valtech Netherlands

February 15, 2019

Tips for Episerver logging

Some of you may have noticed I quite like Serilog (https://serilog.net). And my NuGet package (https://nuget.episerver.com/package/?id=EPi.Libraries.Logging.Serilog) to integrate it in Episerver is used quite a few times, so I guess I am not the only one.

One of the reasons I like it is that you enrich your logging with a lot of extra information.

There are quite a few enrichers available, but none for Episerver specific things.

For example if you want to log the SiteId when an error occurs, you can create the following LogEventEnricher

 

public class SiteDataEnricher : ILogEventEnricher

{

    public const string SiteIdPropertyName = "SiteId";

 

    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)

    {

        if (logEvent == null)

        {

            return;

        }

 

        SiteDefinition siteDefinition = SiteDefinition.Current;

 

        if (siteDefinition.Id != Guid.Empty)

        {

            logEvent.AddPropertyIfAbsent(

            new LogEventProperty(

            name: SiteIdPropertyName,

            value: new ScalarValue(value: SiteDefinition.Current.Id)));

        }

    }

}

 

I have created two NuGet packages enabling you to log

for CMS: Site Id, Site Name, Site Url, the serialized Content Data, Content Id and the Preferred Culture

for Commerce: the serialized Cart (if you call it default), current Contact Id, current Contact Name, current Contact Email, current Market

 

The complete code can be found on GitHub (https://github.com/jstemerdink/EPi.Libraries.Logging.Serilog.Enrichers)

Or you can install the NuGet packages (https://nuget.episerver.com/?q=EPi.Libraries.Logging.Serilog.Enrichers) from the Episerver feed.

 

If you have other things “log worthy” let me know, or add it to the modules on GitHub.

 

Original article was posted on EpiServer Development: Enrich your logging with Episerver data 

 

Meet The Challenges Of Today's Digital Economy

Ready to take that first step and rise to your digital potential? Contact Valtech today.
Talk to Us