Skip to content

Commit

Permalink
add CorrelationIdLogEventEnricher
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Feb 19, 2019
1 parent e3117cd commit 9e9d85d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions abp_io/src/Volo.AbpWebSite.Web/AbpWebSiteWebModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();

app.UseCorrelationId();

app.UseAbpRequestLocalization();

if (env.IsDevelopment())
Expand Down
28 changes: 28 additions & 0 deletions abp_io/src/Volo.AbpWebSite.Web/CorrelationIdLogEventEnricher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Serilog.Core;
using Serilog.Events;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Tracing;

namespace Volo.AbpWebSite
{
//This is for trial for now
public class CorrelationIdLogEventEnricher : ILogEventEnricher, ITransientDependency
{
private readonly ICorrelationIdProvider _correlationIdProvider;

public CorrelationIdLogEventEnricher(ICorrelationIdProvider correlationIdProvider)
{
_correlationIdProvider = correlationIdProvider;
}

public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddOrUpdateProperty(
new LogEventProperty(
"CorrelationId",
new ScalarValue("CorrId:" + _correlationIdProvider.Get())
)
);
}
}
}
3 changes: 2 additions & 1 deletion abp_io/src/Volo.AbpWebSite.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
.AddDebug()
.AddSerilog(new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.File("Logs/logs.txt")
.Enrich.With(app.ApplicationServices.GetRequiredService<CorrelationIdLogEventEnricher>())
.WriteTo.File("Logs/logs.txt",outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] [{CorrelationId}] {Message:lj}{NewLine}{Exception}")
.CreateLogger()
);

Expand Down

0 comments on commit 9e9d85d

Please sign in to comment.