Enrich your logging with Episerver data

Some of you may have noticed I quite like Serilog. And my NuGet package 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

Or you can install the NuGet packages from the Episerver feed.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s