diff --git a/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs b/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs index 992f139..5021511 100644 --- a/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs +++ b/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs @@ -9,57 +9,10 @@ namespace SerilogEcsLogging.Logging; public static class HostBuilderExtensions { - 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) => new EcsTextFormatter(new EcsTextFormatterConfiguration().MapCustom(EcsMapper.MapLogEvent).MapExceptions(true).MapCurrentThread(true).MapHttpContext(context.Configuration.Get())); - public static IHostBuilder UseSerilogEvents(this IHostBuilder builder, Action? configureLogger = null, bool logEcsEvents = true, bool logToConsole = true, bool consoleToStdErr = false, string? logFilePath = null) { return builder.UseSerilog((context, configuration) => { - 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)); - } - } - + configuration.ConfigureEcs(logEcsEvents, logToConsole, consoleToStdErr, logFilePath, context); configuration.ReadFrom.Configuration(context.Configuration); configureLogger?.Invoke(context, configuration); }); diff --git a/src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs b/src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs new file mode 100644 index 0000000..d802b74 --- /dev/null +++ b/src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs @@ -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()); + } + 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; + } +} \ No newline at end of file diff --git a/src/SerilogEcsLogging/SerilogEcsLogging.csproj b/src/SerilogEcsLogging/SerilogEcsLogging.csproj index 129ccf1..936bc9c 100644 --- a/src/SerilogEcsLogging/SerilogEcsLogging.csproj +++ b/src/SerilogEcsLogging/SerilogEcsLogging.csproj @@ -7,7 +7,7 @@ true mrapavy https://github.com/mrapavy/serilog-ecs-logging - 1.0.6 + 1.0.7