IHost does not release log file after Dispose()

2 weeks ago 9
ARTICLE AD BOX

I'm trying to read a log file that was written from a custom ILoggerProvider in a dotnet application:

HostApplicationBuilder builder = Host.CreateApplicationBuilder(); builder.Logging.ClearProviders(); builder.Logging.AddCustomLogger(logFilePath); using (IHost host = builder.Build()) { host.RunAsync(); var hostLogger = host.Services.GetRequiredService<ILogger<Program>>(); hostLogger.LogDebug("debug"); } string readLog = File.ReadAllText(logFilePath, Encoding.Unicode); Console.WriteLine(readLog);

In my custom logger provider, I open the file in Append mode:

_logFileStream = new FileStream(logFilePath, new FileStreamOptions() { Access = FileAccess.Write, Mode = FileMode.Append, Share = FileShare.ReadWrite });

And close/dispose it:

public override void Dispose() { if (_logFileStream != null) { _logFileStream.Close(); _logFileStream.Dispose(); } }

However, when I try to read this file after the using should have disposed the IHost it's still locked (The process cannot access the file because it is being used by another process.)

I've tried adding a shutdown/wait to the host:

host.StopAsync(); host.WaitForShutdown();

I've even tried running GC.Collect(), but still the file remains locked by the host process.

How can I get this IHost to let go of my file?

Read Entire Article