serilog-contrib / serilog-sinks-applicationinsights

A Serilog sink that writes events to Microsoft Azure Application Insights
Apache License 2.0
220 stars 72 forks source link

Application insight not working when keyed services are added - No service for type TelemetryConfiguration #227

Closed knilecrack closed 2 months ago

knilecrack commented 2 months ago

Hello,

I just wanted to post this here in case someone else runs into same issue as I did, and not lose a day or two trying to figure out what's happening. In case this should not be here feel free to close it

This is in particular bug with App insights and has nothing to do with serilog. The problem is when there are keyed services registered in DI (.net 8 feature) TelemetryConfiguration won't be registered and if you are using something like this

Log.Logger = new LoggerConfiguration()
    .WriteTo.ApplicationInsights(
        serviceProvider.GetRequiredService<TelemetryConfiguration>(),
    TelemetryConverter.Traces)
    .CreateLogger();

to configure serilog it will throw an exception System.InvalidOperationException: 'No service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' has been registered.

you can find more information here

I can see that for some people workaround was to register App insight before any other services.

I've experienced this issue with following versions: Serilog.Sinks.ApplicationInsights - 4.0.0 Serilog.AspNetCore - 8.0.2

nblumhardt commented 2 months ago

Thanks @knilecrack :+1: