diff --git a/src/SerilogEcsLogging/Logging/EcsEvent.cs b/src/SerilogEcsLogging/Logging/EcsEvent.cs index 12b3188..d0b43a0 100644 --- a/src/SerilogEcsLogging/Logging/EcsEvent.cs +++ b/src/SerilogEcsLogging/Logging/EcsEvent.cs @@ -3,7 +3,9 @@ public class EcsEvent : IEcsEvent { public string EventAction { get; } - + + public string EventId { get; } + public string EventKind { get; } public IList Tags { get; } @@ -20,9 +22,10 @@ public class EcsEvent : IEcsEvent public string? TraceId { get; set; } - public EcsEvent(string eventAction, string eventKind = Elastic.CommonSchema.EventKind.Event, IList? tags = null, bool? eventOutcome = null, TimeSpan? eventDuration = null, string? message = null, object? eventData = null, string? transactionId = null, string? traceId = null) + public EcsEvent(string eventAction, string eventKind = Elastic.CommonSchema.EventKind.Event, IList? tags = null, bool? eventOutcome = null, TimeSpan? eventDuration = null, string? message = null, object? eventData = null, string? transactionId = null, string? traceId = null, string? eventId = null) { EventAction = eventAction; + EventId = eventId ?? Guid.NewGuid().ToString(); EventKind = eventKind; Tags = tags ?? new List(); EventOutcome = eventOutcome; diff --git a/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs b/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs index ca390ab..f0336e4 100644 --- a/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs +++ b/src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs @@ -12,7 +12,7 @@ public static class HostBuilderExtensions 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) + public static IHostBuilder UseSerilogEvents(this IHostBuilder builder, Action? configureLogger = null, bool logEcsEvents = true, bool logToConsole = true, string? logFilePath = null) { return builder.UseSerilog((context, configuration) => { configuration @@ -41,6 +41,23 @@ public static IHostBuilder UseSerilogEvents(this IHostBuilder builder, Action c.File(CreateEcsTextFormatter(context), logFilePath, rollingInterval: RollingInterval.Day)); + } + else + { + configuration.WriteTo.Async(c => c.File(logFilePath, rollingInterval: RollingInterval.Day, outputTemplate: TraceTemplate)); + } + } + configuration.ReadFrom.Configuration(context.Configuration); configureLogger?.Invoke(context, configuration); }); diff --git a/src/SerilogEcsLogging/Logging/IEcsEvent.cs b/src/SerilogEcsLogging/Logging/IEcsEvent.cs index f063071..1644180 100644 --- a/src/SerilogEcsLogging/Logging/IEcsEvent.cs +++ b/src/SerilogEcsLogging/Logging/IEcsEvent.cs @@ -4,6 +4,8 @@ public interface IEcsEvent { string EventAction { get; } + string EventId { get; } + string EventKind { get; } IList? Tags { get; } diff --git a/src/SerilogEcsLogging/Logging/LoggerExtensions.cs b/src/SerilogEcsLogging/Logging/LoggerExtensions.cs index e3a8b6b..c176045 100644 --- a/src/SerilogEcsLogging/Logging/LoggerExtensions.cs +++ b/src/SerilogEcsLogging/Logging/LoggerExtensions.cs @@ -14,6 +14,7 @@ public static class LoggerExtensions public const string LogMethodName = "LogOriginMethodName"; public const string EventActionSeverity = "ActionSeverity"; public const string EventActionName = "ActionName"; + public const string EventActionId = "ActionId"; public const string EventActionKind = "ActionKind"; public const string EventDuration = "ElapsedMilliseconds"; public const string EventOutcome = "ActionOutcome"; @@ -40,7 +41,7 @@ public static void LogEvent(this ILogger logger, LogLevel logLevel, Exception? e outcome = Elastic.CommonSchema.EventOutcome.Failure; } - using (logger.BeginScope($"{{{LogFilename}}} {{{LogLineNumber}}} {{{LogMethodName}}} {{{EventActionSeverity}}} {{{EventActionName}}} {{{EventActionKind}}} {{{EventDuration}}} {{{EventOutcome}}} {{{EventStart}}} {{@{EventData}}} {{@{Tags}}}", sourceFilePath, sourceLineNumber, memberName, LogLevel.None - logLevel, ecsEvent.EventAction, ecsEvent.EventKind, ecsEvent.EventDuration, outcome, eventStart, ecsEvent.EventData, ecsEvent.Tags)) + using (logger.BeginScope($"{{{LogFilename}}} {{{LogLineNumber}}} {{{LogMethodName}}} {{{EventActionSeverity}}} {{{EventActionName}}} {{{EventActionId}}} {{{EventActionKind}}} {{{EventDuration}}} {{{EventOutcome}}} {{{EventStart}}} {{@{EventData}}} {{@{Tags}}}", sourceFilePath, sourceLineNumber, memberName, LogLevel.None - logLevel, ecsEvent.EventAction, ecsEvent.EventId, ecsEvent.EventKind, ecsEvent.EventDuration, outcome, eventStart, ecsEvent.EventData, ecsEvent.Tags)) { logger.Log(logLevel, exception, ecsEvent.EventMessage); } diff --git a/src/SerilogEcsLogging/SerilogEcsLogging.csproj b/src/SerilogEcsLogging/SerilogEcsLogging.csproj index 9e0017b..cd048d6 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.3 + 1.0.4