Use Serilog with CMS12

In previous versions of Optimizely, when you wanted to use a different logger, it took a bit of work. Or you could use one of my providers. With CMS12 you can now just configure a different provider without the need of a custom provider.

For Serilog add the following packages, I used the file sink, but you can use whatever sink you want of course:
Serilog
Serilog.AspNetCore
Serilog.Extensions.Logging
Serilog.Settings.Configuration
Serilog.Sinks.File
System.Configuration.ConfigurationManager

In the appsettings.json add the configuration for Serilog

"Serilog": {
    "Using": [ "Serilog.Sinks.File" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "EPiserver": "Information",
        "EPiServer.Commerce": "Debug"
      }
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "./logs/log-.txt",
          "rollingInterval": "Day"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Destructure": [
      {
        "Name": "ToMaximumDepth",
        "Args": { "maximumDestructuringDepth": 4 }
      },
      {
        "Name": "ToMaximumStringLength",
        "Args": { "maximumStringLength": 100 }
      },
      {
        "Name": "ToMaximumCollectionCount",
        "Args": { "maximumCollectionCount": 10 }
      }
    ],
    "Properties": {
      "Application": "Core Commerce"
    }
  }

In the appsettings.json remove the default logging settings, which probably looks something like this

"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "EPiServer": "Debug"
      "EPiServer.Commerce": "Debug"
    }
  }

In your program.cs add a configuration property

public static IConfiguration Configuration { get; } = 
            new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appSettings.json", false, true)
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", true)
            .AddEnvironmentVariables()
            .Build();

Create the logger in “main”

Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration: Configuration)
                .CreateLogger();

In the “hostbuilder” add Serilog

return Host.CreateDefaultBuilder(args: args)
                    .ConfigureCmsDefaults()
                    .UseSerilog()
                    .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

That’s it. Whether you still want to use the old LogManager implementation or the logging framework of .Net 5 (see https://world.optimizely.com/documentation/developer-guides/CMS/logging/) Serilog will be used.

You can find a gist here.

This means that my custom logging providers won’t need to be updated, you can just use the default implementation now.

5 thoughts on “Use Serilog with CMS12

  1. Hi Jeroen,

    This article is really helpful for me to set up Serilog in CMS 12. What will happen if we deployed in DXP with Serilog and Where we can see the log in DXP?

    Like

    1. DXP requires Application Insights, so that sink should be there at the least. I don’t know if there are specific requirements for the application insights settings though. And if I remember correctly DXP doesn’t allow you to change the logging. But that was before .Net 5. I will try to find out

      Like

      1. Thank you Jeroen respond to my concerns. As per my understanding, we don’t have any control like database connection, Application Insights including exception message once deployed in DXP, especially in CMS 12. So, in that case, we don’t need to add Serilog in Release mode. Also, I am not sure where we can see the exception message in DXP. Please provide any more details about exception logging if you have.

        Like

Leave a comment