diff --git a/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs b/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs index aae63c8..b65290a 100644 --- a/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs +++ b/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs @@ -1,3 +1,4 @@ +using System.Runtime.InteropServices; using Microsoft.Extensions.Hosting; using Serilog; @@ -5,6 +6,10 @@ namespace SerilogEcsLogging.Logging; public static class HostBuilderExtensions { + public static string DefaultLogDirectory => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"{Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData, Environment.SpecialFolderOption.Create)}/EFTLog" : "/var/log"; + + public static bool RunningInContainer => string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), "true", StringComparison.InvariantCultureIgnoreCase); + 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) => { diff --git a/src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs b/src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs index f402cf8..05e26ae 100644 --- a/src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs +++ b/src/SerilogEcsLogging/Logging/LoggerConfigurationExtensions.cs @@ -1,4 +1,5 @@ -using Elastic.CommonSchema.Serilog; +using System.Reflection; +using Elastic.CommonSchema.Serilog; using Serilog; using Serilog.Events; @@ -8,7 +9,7 @@ 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() => new EcsTextFormatter(new EcsTextFormatterConfiguration { + public static EcsTextFormatter EcsTextFormatter => new EcsTextFormatter(new EcsTextFormatterConfiguration { IncludeHost = true, IncludeProcess = true, IncludeUser = true, @@ -16,7 +17,7 @@ public static class LoggerConfigurationExtensions LogEventPropertiesToFilter = new HashSet {"metadata.*", "labels.*"} }); - public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configuration, bool logEcsEvents = true, bool logToConsole = true, bool consoleToStdErr = false, string? logFilePath = null) + public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configuration, bool logEcsEvents = true, bool logToConsole = true, bool consoleToStdErr = false, string? logFilePath = null, string? httpEndpoint = null, TimeSpan? httpPostPeriod = null, int? logEventsInHttpBatchLimit = 1000) { configuration .Enrich.FromLogContext() @@ -29,14 +30,14 @@ public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configur .Enrich.WithCorrelationId() .Enrich.WithAssemblyName() .Enrich.WithAssemblyVersion(); - + if (logToConsole) { LogEventLevel? stdErrFromLevel = consoleToStdErr ? LogEventLevel.Verbose : null; configuration.WriteTo.Async(c => { if (logEcsEvents) { - c.Console(CreateEcsTextFormatter(), standardErrorFromLevel: stdErrFromLevel); + c.Console(EcsTextFormatter, standardErrorFromLevel: stdErrFromLevel); } else { @@ -54,7 +55,7 @@ public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configur var extension = Path.GetExtension(logFilePath); logFilePath = Path.Combine(directory, $"{filename}.ECS.{extension}"); - configuration.WriteTo.Async(c => c.File(CreateEcsTextFormatter(), logFilePath, rollingInterval: RollingInterval.Day)); + configuration.WriteTo.Async(c => c.File(EcsTextFormatter, logFilePath, rollingInterval: RollingInterval.Day)); } else { @@ -62,6 +63,18 @@ public static LoggerConfiguration ConfigureEcs(this LoggerConfiguration configur } } + if (!string.IsNullOrEmpty(httpEndpoint)) + { + var path = logFilePath; + if (string.IsNullOrEmpty(path)) + { + var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly(); + path = Path.Combine(HostBuilderExtensions.DefaultLogDirectory, assembly.GetName().Name ?? Guid.NewGuid().ToString("N")); + } + + configuration.WriteTo.DurableHttpUsingFileSizeRolledBuffers(httpEndpoint, $"{path}-buffer", textFormatter: logEcsEvents ? EcsTextFormatter : null, period: httpPostPeriod, logEventsInBatchLimit: logEventsInHttpBatchLimit); + } + return configuration; } } \ No newline at end of file diff --git a/src/SerilogEcsLogging/SerilogEcsLogging.csproj b/src/SerilogEcsLogging/SerilogEcsLogging.csproj index 7278567..a50b57d 100644 --- a/src/SerilogEcsLogging/SerilogEcsLogging.csproj +++ b/src/SerilogEcsLogging/SerilogEcsLogging.csproj @@ -1,28 +1,29 @@ - net6.0 enable enable true mrapavy https://github.com/mrapavy/serilog-ecs-logging - 1.1.6 + 1.1.7 + net8.0 - + - + - - + + - + +