Skip to content

Commit

Permalink
Added EventActionId; Added option to log to file
Browse files Browse the repository at this point in the history
  • Loading branch information
mrapavy committed Nov 1, 2023
1 parent 6aa416e commit a05d206
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/SerilogEcsLogging/Logging/EcsEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
public class EcsEvent : IEcsEvent
{
public string EventAction { get; }


public string EventId { get; }

public string EventKind { get; }

public IList<string> Tags { get; }
Expand All @@ -20,9 +22,10 @@ public class EcsEvent : IEcsEvent

public string? TraceId { get; set; }

public EcsEvent(string eventAction, string eventKind = Elastic.CommonSchema.EventKind.Event, IList<string>? 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<string>? 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<string>();
EventOutcome = eventOutcome;
Expand Down
19 changes: 18 additions & 1 deletion src/SerilogEcsLogging/Logging/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpContextAccessor>()));

public static IHostBuilder UseSerilogEvents(this IHostBuilder builder, Action<HostBuilderContext, LoggerConfiguration>? configureLogger = null, bool logEcsEvents = true, bool logToConsole = true)
public static IHostBuilder UseSerilogEvents(this IHostBuilder builder, Action<HostBuilderContext, LoggerConfiguration>? configureLogger = null, bool logEcsEvents = true, bool logToConsole = true, string? logFilePath = null)
{
return builder.UseSerilog((context, configuration) => {
configuration
Expand Down Expand Up @@ -41,6 +41,23 @@ public static IHostBuilder UseSerilogEvents(this IHostBuilder builder, Action<Ho
});
}

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.ReadFrom.Configuration(context.Configuration);
configureLogger?.Invoke(context, configuration);
});
Expand Down
2 changes: 2 additions & 0 deletions src/SerilogEcsLogging/Logging/IEcsEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public interface IEcsEvent
{
string EventAction { get; }

string EventId { get; }

string EventKind { get; }

IList<string>? Tags { get; }
Expand Down
3 changes: 2 additions & 1 deletion src/SerilogEcsLogging/Logging/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}
Expand Down
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.3</Version>
<Version>1.0.4</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit a05d206

Please sign in to comment.