Skip to content

Commit

Permalink
Moved Serilog LoggerConfiguration to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
mrapavy committed Dec 14, 2023
1 parent 39685db commit 9d9d2df
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 49 deletions.
49 changes: 1 addition & 48 deletions src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpContextAccessor>()));

public static IHostBuilder UseSerilogEvents(this IHostBuilder builder, Action<HostBuilderContext, LoggerConfiguration>? 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);
});
Expand Down
72 changes: 72 additions & 0 deletions src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs
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;
}
}
2 changes: 1 addition & 1 deletion src/SerilogEcsLogging/SerilogEcsLogging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>mrapavy</Authors>
<PackageProjectUrl>https://github.com/mrapavy/serilog-ecs-logging</PackageProjectUrl>
<Version>1.0.6</Version>
<Version>1.0.7</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 9d9d2df

Please sign in to comment.