serilog-sinks-applicationinsights
serilog-sinks-applicationinsights copied to clipboard
Support the new TraceId and SpanId in Serilog
Describe your suggestion Serilog 3.1 adds first-class support for TraceId and SpanId: https://github.com/serilog/serilog/pull/1955
Each sink has to implement this.
This would require upgrading to the latest version of Serilog. This package is using an outdated version which we, as consumers, would like to get upgraded as well
10 months later; I guess this project is dead...?
Not dead, but needs contributions. It's a high-use package, so I guess the main thing holding up PRs for these kinds of things is the effort involved in testing and pulling something together at a suitable level of quality. A PR would be very welcome if anyone following this thread is keen.
I ran into the need of having support for those SpanId and TraceId using the Serilog ApplicationInsights sink. I finally make that works so here's the steps i used, hope it would help next peoples coming here until a patch is published 😉
:one: Create a custom converter (whether a Trace or Event one depending on your usage) :
public class MyCustomTraceTelemetryConverter : TraceTelemetryConverter
{
public override void ForwardPropertiesToTelemetryProperties(LogEvent logEvent,
ISupportProperties telemetryProperties, IFormatProvider formatProvider)
{
if (logEvent.SpanId is not null)
{
telemetryProperties.Properties.Add("SpanId", logEvent.SpanId.ToString());
}
if (logEvent.TraceId is not null)
{
telemetryProperties.Properties.Add("TraceId", logEvent.TraceId.ToString());
}
base.ForwardPropertiesToTelemetryProperties(logEvent, telemetryProperties, formatProvider,
includeLogLevel: true,
includeRenderedMessage: true,
includeMessageTemplate: true);
}
}
:bulb: Basically SpanId and TraceId are already included in LogEvent but not sended to telemetryProperties (which are sended to your AppInsights instance)
:warning: As you're overriding the ForwardPropertiesToTelemetryProperties method from the converter you have to manually specify whether you want to include LogLevel, RenderedMessage and MessageTemplate specific properties.
:second: Add your sink to your serilog configuration using your brandly new converter
:bulb: I didn't used configuration file for this you can surely do. Also you can enhance this config with anything else you need (other enrichers, log context, etc.)
var appInsightsConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
builder.Services.AddSerilog((_, loggerConfiguration) =>
{
loggerConfiguration
.WriteTo.ApplicationInsights(
connectionString: appInsightsConnectionString,
telemetryConverter: new MyCustomTraceTelemetryConverter());
})
🚀 Et voilà
This PR was merged several months ago, but there isn't a stable release available. Are there plans to publish a stable version?
Thanks for the nudge, @jblackburn21 👍
Does the 4.1.0-dev-02304 version work for you as required? I've queued up https://github.com/serilog-contrib/serilog-sinks-applicationinsights/pull/235 and will merge if someone's able to confirm that the current dev package is good. (I don't personally run App Insights anywhere, so no good for dog fooding :-))
That dev version worked for us.
Thanks @pinkfloydx33! Merging now 👍