From a5684f867a7da71142ef50ec5e9df6d60abd90ae Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Wed, 1 May 2019 15:15:29 +0200 Subject: [PATCH] #5373 - Added custom extension method to configure the file serilog file sink to be wrapped in the async sink --- .../Logging/Serilog/LoggerConfigExtensions.cs | 64 +++++++++++++++++-- .../config/serilog.Release.config | 3 +- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Core/Logging/Serilog/LoggerConfigExtensions.cs b/src/Umbraco.Core/Logging/Serilog/LoggerConfigExtensions.cs index 200b2fadea..ccb74f0c15 100644 --- a/src/Umbraco.Core/Logging/Serilog/LoggerConfigExtensions.cs +++ b/src/Umbraco.Core/Logging/Serilog/LoggerConfigExtensions.cs @@ -1,7 +1,11 @@ using System; +using System.Text; using System.Web; using Serilog; +using Serilog.Configuration; +using Serilog.Core; using Serilog.Events; +using Serilog.Formatting; using Serilog.Formatting.Compact; using Umbraco.Core.Logging.Serilog.Enrichers; @@ -35,7 +39,7 @@ namespace Umbraco.Core.Logging.Serilog .Enrich.With() .Enrich.With() .Enrich.With(); - + return logConfig; } @@ -50,15 +54,63 @@ namespace Umbraco.Core.Logging.Serilog //Main .txt logfile - in similar format to older Log4Net output //Ends with ..txt as Date is inserted before file extension substring logConfig.WriteTo.File($@"{AppDomain.CurrentDomain.BaseDirectory}\App_Data\Logs\UmbracoTraceLog.{Environment.MachineName}..txt", - shared: true, - rollingInterval: RollingInterval.Day, - restrictedToMinimumLevel: minimumLevel, - retainedFileCountLimit: null, //Setting to null means we keep all files - default is 31 days - outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss,fff} [P{ProcessId}/D{AppDomainId}/T{ThreadId}] {Log4NetLevel} {SourceContext} - {Message:lj}{NewLine}{Exception}"); + shared: true, + rollingInterval: RollingInterval.Day, + restrictedToMinimumLevel: minimumLevel, + retainedFileCountLimit: null, //Setting to null means we keep all files - default is 31 days + outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss,fff} [P{ProcessId}/D{AppDomainId}/T{ThreadId}] {Log4NetLevel} {SourceContext} - {Message:lj}{NewLine}{Exception}"); return logConfig; } + + /// + /// Used in config - If renamed or moved to other assembly the config file also has be updated. + /// + public static LoggerConfiguration File(this LoggerSinkConfiguration configuration, ITextFormatter formatter, + string path, + bool shared = false, + bool buffered = false, + LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose, + LoggingLevelSwitch levelSwitch = null, + long? fileSizeLimitBytes = 1073741824, + TimeSpan? flushToDiskInterval = null, + RollingInterval rollingInterval = RollingInterval.Infinite, + bool rollOnFileSizeLimit = false, + int? retainedFileCountLimit = 31, + Encoding encoding = null, + bool async = false, + bool asyncBlockWhenFull = false, + int asyncBufferSize = 10000) + { + LoggerConfiguration GetFileSink(LoggerSinkConfiguration c) + { + return c.File( + formatter, + path, + restrictedToMinimumLevel, + fileSizeLimitBytes, + levelSwitch, + buffered, + shared, + flushToDiskInterval, + rollingInterval, + rollOnFileSizeLimit, + retainedFileCountLimit, + encoding); + } + + if ( async ) + { + + return configuration.Async(c => GetFileSink(c), blockWhenFull: asyncBlockWhenFull, bufferSize: asyncBufferSize); + } + else + { + return GetFileSink(configuration); + } + } + /// /// Outputs a CLEF format JSON log at /App_Data/Logs/ /// diff --git a/src/Umbraco.Web.UI/config/serilog.Release.config b/src/Umbraco.Web.UI/config/serilog.Release.config index 99897cfa0a..b5dd1a2e06 100644 --- a/src/Umbraco.Web.UI/config/serilog.Release.config +++ b/src/Umbraco.Web.UI/config/serilog.Release.config @@ -17,13 +17,14 @@ - + +