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

Cannot access a disposed object, Object name: 'ApplicationInsightsSink' #136

Closed slanto closed 2 years ago

slanto commented 4 years ago

Hi, I am struggling with writing logs to App Insights from ASP.NET MVC application. I get the following exception:

Caught exception while emitting to sink Serilog.Sinks.ApplicationInsights.ApplicationInsightsSink: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'ApplicationInsightsSink'.
   at Serilog.Sinks.ApplicationInsights.ApplicationInsightsSink.CheckForAndThrowIfDisposed()
   at Serilog.Sinks.ApplicationInsights.ApplicationInsightsSink.Emit(LogEvent logEvent)
   at Serilog.Core.Sinks.SafeAggregateSink.Emit(LogEvent logEvent)

This is my logger configuration where I write LogEvent to Traces in Application Insights.

   Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Is(minimumLevel)
                .Enrich.FromLogContext()
                .Enrich.WithMachineName()
                .Enrich.WithProcessId()
                .Enrich.WithProcessName()
                .Enrich.WithProperty(LoggerFields.ApplicationName, appName)                
                .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces)                
                .CreateLogger();

Because of the mentioned error, there are no any logs with Trace type in my Application Insights. Do you have any suggestions?

The following lib's version is used:

angularsen commented 3 years ago

I'm seeing the same with the same versions of nugets, on both netcoreapp2.2 and netcoreapp3.1. I am resolving TelemetryClient from host service provider on startup, also tried resolving TelemetryClient instead with no luck.

Update: Seems we had configured ILogger as transient dependency in order to look up the latest global logger instance via Log.Logger, since we had to change the instance during startup when we could resolve the TelemetryClient dependency. I'm guessing this results in asp.net disposing the logger and thus the Application Insights sink.

nblumhardt commented 2 years ago

Hi! 👋 Thanks for getting in touch, and sorry we were unable to respond to your request. We're in the process of bringing new maintainers onto this project, and need to close stale tickets so that we can identify which items are still important to users of the sink.

If this issue still affects you:

Thanks again!

Steve887 commented 2 years ago

I am getting this error trying to log to app insights using version 4.0.0. Was there ever a resolution to this?