-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Serilog LoggerConfiguration to separate class
- Loading branch information
Showing
3 changed files
with
74 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Elastic.CommonSchema.Serilog; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
namespace SerilogEcsLogging.Logging; | ||
|
||
public static class LoggerConfigurationExtensions | ||
{ | ||
public const string TraceTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}][{MachineName}][{Level:u3}][{SourceContext}][{ThreadId}]{Scope} {Message}{NewLine}{Exception}"; | ||
|
||
public static EcsTextFormatter CreateEcsTextFormatter(HostBuilderContext? context = null) | ||
{ | ||
var configuration = new EcsTextFormatterConfiguration().MapCustom(EcsMapper.MapLogEvent).MapExceptions(true).MapCurrentThread(true); | ||
if (context != null) | ||
{ | ||
configuration.MapHttpContext(context.Configuration.Get<HttpContextAccessor>()); | ||
} | ||
return new EcsTextFormatter(configuration); | ||
} | ||
|
||
public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configuration, bool logEcsEvents = true, bool logToConsole = true, bool consoleToStdErr = false, string? logFilePath = null, HostBuilderContext? context = null) | ||
{ | ||
configuration | ||
.Enrich.FromLogContext() | ||
.Enrich.WithMachineName() | ||
.Enrich.WithEnvironmentName() | ||
.Enrich.WithEnvironmentUserName() | ||
.Enrich.WithProcessId() | ||
.Enrich.WithProcessName() | ||
.Enrich.WithThreadId() | ||
.Enrich.WithCorrelationId() | ||
.Enrich.WithAssemblyName() | ||
.Enrich.WithAssemblyVersion(); | ||
|
||
if (logToConsole) | ||
{ | ||
LogEventLevel? stdErrFromLevel = consoleToStdErr ? LogEventLevel.Verbose : null; | ||
configuration.WriteTo.Async(c => { | ||
if (logEcsEvents) | ||
{ | ||
c.Console(CreateEcsTextFormatter(context), standardErrorFromLevel: stdErrFromLevel); | ||
} | ||
else | ||
{ | ||
c.Console(outputTemplate: TraceTemplate, standardErrorFromLevel: stdErrFromLevel); | ||
} | ||
}); | ||
} | ||
|
||
if (logFilePath != null) | ||
{ | ||
if (logEcsEvents) | ||
{ | ||
var directory = Path.GetDirectoryName(logFilePath) ?? string.Empty; | ||
var filename = Path.GetFileNameWithoutExtension(logFilePath); | ||
var extension = Path.GetExtension(logFilePath); | ||
logFilePath = Path.Combine(directory, $"{filename}.ECS.{extension}"); | ||
|
||
configuration.WriteTo.Async(c => c.File(CreateEcsTextFormatter(context), logFilePath, rollingInterval: RollingInterval.Day)); | ||
} | ||
else | ||
{ | ||
configuration.WriteTo.Async(c => c.File(logFilePath, rollingInterval: RollingInterval.Day, outputTemplate: TraceTemplate)); | ||
} | ||
} | ||
|
||
return configuration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters