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?
Disposebut doesn't callbase.Dispose? Is there another derived type that hasn't calledbase.Disposeyet? Do you know if thisDisposehas been called?RunAsync, i.e.await host.RunAsync();. So the host could be disposed before it's done running. Is that a mistake? Is there any chance that, somehow, the log file is getting opened by some async code after, rather than before, the host is disposed?FileShare.ReadWrite, the file can be opened by others.