Last active
December 23, 2022 21:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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