Skip to content

System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'The log file has been disposed.'.' #2176

@khteh

Description

@khteh

The Serilog maintainers want you to have a great experience using Serilog, and will happily track down and resolve bugs. We all have limited time, though, so please think through all of the factors that might be involved and include as much useful information as possible 😊.

ℹ If the problem is caused by a sink or other extension package, please track down the correct repository for that package and create the report there: this tracker is for the core Serilog package only.

Description
What's going wrong?
I have the following code snippet in Program.cs:

    builder.Host.UseSerilog((ctx, svc, config) =>
    {
        config.ReadFrom.Configuration(ctx.Configuration).ReadFrom.Services(svc).Enrich.FromLogContext();
    }, preserveStaticLogger: true);
    builder.Services.AddLogging(builder => builder.AddSerilog(dispose: true));

Debug session hits the following exception:

 	Serilog.Sinks.File.dll!Serilog.Sinks.File.RollingFileSink.Emit(Serilog.Events.LogEvent logEvent)	Unknown
>	Serilog.dll!Serilog.Core.Sinks.SafeAggregateSink.Emit(Serilog.Events.LogEvent logEvent) Line 33	C#
 	Serilog.dll!Serilog.Core.Logger.Dispatch(Serilog.Events.LogEvent logEvent) Line 481	C#
 	Serilog.dll!Serilog.Core.Logger.Serilog.Core.ILogEventSink.Emit(Serilog.Events.LogEvent logEvent) Line 465	C#
 	Serilog.dll!Serilog.Core.Logger.Dispatch(Serilog.Events.LogEvent logEvent) Line 481	C#
 	Serilog.dll!Serilog.Core.Logger.Serilog.Core.ILogEventSink.Emit(Serilog.Events.LogEvent logEvent) Line 465	C#
 	Serilog.dll!Serilog.Core.Logger.Dispatch(Serilog.Events.LogEvent logEvent) Line 481	C#
 	Serilog.dll!Serilog.Core.Logger.Write(Serilog.Events.LogEvent logEvent) Line 456	C#
 	Serilog.Extensions.Logging.dll!Serilog.Extensions.Logging.SerilogLogger.Log<Microsoft.Extensions.Logging.FormattedLogValues>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, Microsoft.Extensions.Logging.FormattedLogValues state, System.Exception exception, System.Func<Microsoft.Extensions.Logging.FormattedLogValues, System.Exception, string> formatter) Line 85	C#
 	Microsoft.Extensions.Logging.Abstractions.dll!Microsoft.Extensions.Logging.LoggerExtensions.Log(Microsoft.Extensions.Logging.ILogger logger, Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, System.Exception exception, string message, object[] args) Line 486	C#
 	Microsoft.Extensions.Logging.Abstractions.dll!Microsoft.Extensions.Logging.LoggerExtensions.LogInformation(Microsoft.Extensions.Logging.ILogger logger, string message, object[] args) Line 222	C#
 	Web.Api.dll!Program.<Main>$.AnonymousMethod__26() Line 476	C#
 	System.Private.CoreLib.dll!System.Threading.CancellationTokenSource.Invoke(System.Delegate d, object state, System.Threading.CancellationTokenSource source) Line 994	C#
 	System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 179	C#
 	System.Private.CoreLib.dll!System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(bool throwOnFirstException) Line 813	C#
 	Microsoft.Extensions.Hosting.dll!Microsoft.Extensions.Hosting.Internal.ApplicationLifetime.NotifyStopped()	Unknown
 	Microsoft.Extensions.Hosting.dll!Microsoft.Extensions.Hosting.Internal.Host.StopAsync(System.Threading.CancellationToken cancellationToken)	Unknown
 	Microsoft.Extensions.Hosting.Abstractions.dll!Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.WaitForShutdownAsync(Microsoft.Extensions.Hosting.IHost host, System.Threading.CancellationToken token) Line 115	C#
 	[Resuming Async Method]	
 	System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Threading.Tasks.VoidTaskResult>.AsyncStateMachineBox<Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<WaitForShutdownAsync>d__5>.ExecutionContextCallback(object s) Line 292	C#
 	System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 179	C#
 	System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Threading.Tasks.VoidTaskResult>.AsyncStateMachineBox<Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<WaitForShutdownAsync>d__5>.MoveNext(System.Threading.Thread threadPoolThread) Line 366	C#
 	System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Threading.Tasks.VoidTaskResult>.AsyncStateMachineBox<Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<WaitForShutdownAsync>d__5>.MoveNext() Line 350	C#
 	System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__12_0(System.Action innerContinuation, System.Threading.Tasks.Task innerTask) Line 275	C#
 	System.Private.CoreLib.dll!System.Threading.Tasks.AwaitTaskContinuation.System.Threading.IThreadPoolWorkItem.Execute() Line 647	C#
 	System.Private.CoreLib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Line 1120	C#
 	System.Private.CoreLib.dll!System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() Line 128	C#
 	[Native to Managed Transition]	
 	kernel32.dll!00007ffddc7f259d()	Unknown
 	ntdll.dll!00007ffdde68af58()	Unknown
 	[Async Call Stack]	
 	[Async] Microsoft.Extensions.Hosting.Abstractions.dll!Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(Microsoft.Extensions.Hosting.IHost host, System.Threading.CancellationToken token) Line 69	C#

Reproduction
Please provide code samples showing how you're configuring and calling Serilog to produce the behavior.

Expected behavior
No exception.

Relevant package, tooling and runtime versions
What Serilog version are you using, on what platform?

Additional context
ASP.Net Core 9, all nuget packages are the latest versions.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions