Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oleh-zheleznyak/9fea0c10cddd79eb89c8a75830597de5 to your computer and use it in GitHub Desktop.
Save oleh-zheleznyak/9fea0c10cddd79eb89c8a75830597de5 to your computer and use it in GitHub Desktop.
Serilog Log Event enricher that adds System.Diagnostics.Activity to act as OperationId in Application Insights
using System.Diagnostics;
using Serilog.Core;
using Serilog.Events;
namespace YourCompany.YourService.YourProject.Serilog
{
public class OperationIdEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var activity = Activity.Current;
if (activity is null) return;
logEvent.AddPropertyIfAbsent(new LogEventProperty("Operation Id", new ScalarValue(activity.Id)));
logEvent.AddPropertyIfAbsent(new LogEventProperty("Parent Id", new ScalarValue(activity.ParentId)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment