Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Use Serilog in CMS12

Read my blog here

{
"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"
}
}
// Other configuration
}
using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
public class Program
{
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();
public static IHostBuilder CreateHostBuilder(string[] args, bool isDevelopment)
{
if (isDevelopment)
{
return Host.CreateDefaultBuilder(args: args)
.ConfigureCmsDefaults()
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
return Host.CreateDefaultBuilder(args: args)
.ConfigureCmsDefaults()
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
public static int Main(string[] args)
{
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
bool isDevelopment = environment == Environments.Development;
if (isDevelopment)
{
// Development configuration
}
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration: Configuration)
.CreateLogger();
Log.Information("Starting up!");
Log.Information("Logging enabled");
try
{
CreateHostBuilder(args: args, isDevelopment: isDevelopment).Build().Run();
Log.Information("Stopped cleanly");
return 0;
}
catch (Exception ex)
{
Log.Fatal(exception: ex, "An unhandled exception occured during bootstrapping");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment