Skip to content

Instantly share code, notes, and snippets.

@warrenbuckley
Created November 7, 2022 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warrenbuckley/2ca102a9d17516c3b257a967089026d5 to your computer and use it in GitHub Desktop.
Save warrenbuckley/2ca102a9d17516c3b257a967089026d5 to your computer and use it in GitHub Desktop.
Example V9+ Umbraco CMS Serilog Configuration to write to additional File location
{
"$schema" : "./appsettings-schema.json",
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Examine.Lucene.Providers.LuceneIndex": "Debug",
"Examine.BaseIndexProvider": "Debug",
"Examine.Lucene.LoggingReplicationClient": "Debug",
"Examine.Lucene.ExamineReplicator": "Debug"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "warren-logs/log-.json",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
"rollingInterval": "Day",
"buffered": true,
"retainedFileCountLimit": "2"
}
},
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console"
}
]
}
}
]
},
"Umbraco": {
"CMS": {
"Examine": {
"LuceneDirectoryFactory": "TempFileSystemDirectoryFactory"
},
"Global": {
"Smtp": {
//"From": "your@email.here",
//"Host": "localhost",
// "Port": "25"
}
},
"Hosting": {
"Debug": true
},
"RuntimeMinification": {
"useInMemoryCache": true,
"cacheBuster": "Timestamp"
}
}
}
}
@enkelmedia
Copy link

enkelmedia commented Nov 7, 2022

Taking this one step further and making it possible to view the logs in the backoffice log viewer:

Step 1:
Configure a custom File-zink to write to and configure Umbraco to only write Fatal-errors to the "old" log path.

"Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "System": "Warning",
        "SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddleware": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "UmbracoFile",
        "Args": {
          "RestrictedToMinimumLevel" :  "Fatal"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "../Resources/Logs/log-.json",
          "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
          "rollingInterval": "Day",
          "retainedFileCountLimit": "30",
          "buffered": false,
          "shared": false
        }
      }
    ]

NOTE: Added buffered:false and shared:false.

Step 2

Implement your own Umbraco.Cms.Core.Logging.ILoggingConfiguration and return the path to where you're storing the files.

Switch the it in the DI during startup after Umbraco has registered it's dependencies.

services.AddUmbraco(_env, _config)
    .AddBackOffice()
    .AddWebsite()
    .AddComposers()
    .AddIntranetAuthentication()
    .Build();

services.Replace(new ServiceDescriptor(typeof(ILoggingConfiguration), typeof(MyCustomLoggingConfiguration), ServiceLifetime.Singleton));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment