#5236 - Added options to set (and set default as) the log file is closed after each log event
This commit is contained in:
@@ -13,6 +13,7 @@ namespace Umbraco.Core.Logging.Serilog
|
||||
{
|
||||
public static class LoggerConfigExtensions
|
||||
{
|
||||
private const string AppDomainId = "AppDomainId";
|
||||
/// <summary>
|
||||
/// This configures Serilog with some defaults
|
||||
/// Such as adding ProcessID, Thread, AppDomain etc
|
||||
@@ -32,7 +33,7 @@ namespace Umbraco.Core.Logging.Serilog
|
||||
.Enrich.WithProcessId()
|
||||
.Enrich.WithProcessName()
|
||||
.Enrich.WithThreadId()
|
||||
.Enrich.WithProperty("AppDomainId", AppDomain.CurrentDomain.Id)
|
||||
.Enrich.WithProperty(AppDomainId, AppDomain.CurrentDomain.Id)
|
||||
.Enrich.WithProperty("AppDomainAppId", HttpRuntime.AppDomainAppId.ReplaceNonAlphanumericChars(string.Empty))
|
||||
.Enrich.WithProperty("MachineName", Environment.MachineName)
|
||||
.Enrich.With<Log4NetLevelMapperEnricher>()
|
||||
@@ -81,7 +82,8 @@ namespace Umbraco.Core.Logging.Serilog
|
||||
Encoding encoding = null,
|
||||
bool async = false,
|
||||
bool asyncBlockWhenFull = false,
|
||||
int asyncBufferSize = 10000)
|
||||
int asyncBufferSize = 10000,
|
||||
bool asyncKeepFileOpen = true)
|
||||
{
|
||||
LoggerConfiguration GetFileSink(LoggerSinkConfiguration c)
|
||||
{
|
||||
@@ -100,17 +102,36 @@ namespace Umbraco.Core.Logging.Serilog
|
||||
encoding);
|
||||
}
|
||||
|
||||
if ( async )
|
||||
if (!async && !asyncKeepFileOpen)
|
||||
{
|
||||
throw new InvalidOperationException($"{nameof(asyncKeepFileOpen)} cannot be changed if async is false.");
|
||||
}
|
||||
if (!async && rollOnFileSizeLimit)
|
||||
{
|
||||
throw new InvalidOperationException($"{nameof(rollOnFileSizeLimit)} cannot be changed if async is false.");
|
||||
}
|
||||
|
||||
return configuration.Async(c => GetFileSink(c), blockWhenFull: asyncBlockWhenFull, bufferSize: asyncBufferSize);
|
||||
}
|
||||
else
|
||||
if (async)
|
||||
{
|
||||
return GetFileSink(configuration);
|
||||
Action<LoggerSinkConfiguration> configure;
|
||||
if (asyncKeepFileOpen)
|
||||
{
|
||||
configure = a => GetFileSink(a);
|
||||
}
|
||||
else
|
||||
{
|
||||
// This is a way to force the file to be closed after each log event. Read more https://github.com/serilog/serilog-sinks-file/issues/99#issuecomment-488612711
|
||||
// We basically map all log events with a AppDomainId property (all events) into a file sink and forces the sink to be closed, due to the sinkMapCountLimit = 0.
|
||||
configure = a => a.Map(AppDomainId, (name,c) => GetFileSink(c), sinkMapCountLimit:0);
|
||||
}
|
||||
|
||||
return configuration.Async(configure, blockWhenFull: asyncBlockWhenFull, bufferSize: asyncBufferSize);
|
||||
|
||||
}
|
||||
return GetFileSink(configuration);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Outputs a CLEF format JSON log at /App_Data/Logs/
|
||||
/// </summary>
|
||||
|
||||
@@ -55,6 +55,12 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<!-- note: NuGet deals with transitive references now -->
|
||||
<PackageReference Include="Serilog.Sinks.Async">
|
||||
<Version>1.3.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Serilog.Sinks.Map">
|
||||
<Version>1.0.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Umbraco.Code">
|
||||
<Version>1.0.5</Version>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
|
||||
@@ -20,11 +20,12 @@
|
||||
<add key="serilog:using:File" value="Umbraco.Core" />
|
||||
<add key="serilog:write-to:File.formatter" value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />
|
||||
<add key="serilog:write-to:File.path" value="%BASEDIR%\App_Data\Logs\UmbracoTraceLog.%MACHINENAME%..json" />
|
||||
<add key="serilog:write-to:File.shared" value="true" />
|
||||
<add key="serilog:write-to:File.shared" value="false" />
|
||||
<add key="serilog:write-to:File.restrictedToMinimumLevel" value="Debug" />
|
||||
<add key="serilog:write-to:File.retainedFileCountLimit" value="" /> <!-- Number of log files to keep (or remove value to keep all files) -->
|
||||
<add key="serilog:write-to:File.rollingInterval" value="Day" /> <!-- Create a new log file every Minute/Hour/Day/Month/Year/infinite -->
|
||||
<add key="serilog:write-to:File.async" value="true" />
|
||||
<add key="serilog:write-to:File.asyncKeepFileOpen" value="false" />
|
||||
|
||||
<!-- Optional TXT log file -->
|
||||
<!--<add key="serilog:using:File" value="Serilog.Sinks.File" /> -->
|
||||
|
||||
Reference in New Issue
Block a user