Enrich your logging with Optimizely data

Senior Developer
Valtech Netherlands

februari 15, 2019

Tips for Optimizely logging

Some of you may have noticed I quite like Serilog (https://serilog.net). And my NuGet package (https://nuget.optimizely.com/package/?id=EPi.Libraries.Logging.Serilog) to integrate it in Optimizely 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 Optimizely 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.optimizely.com/?q=EPi.Libraries.Logging.Serilog.Enrichers) from the Optimizely feed.

 

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

 

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

 

Neem contact op

Let's reinvent the future