nreco / logging

Generic file logger for .NET Core (FileLoggerProvider) with minimal dependencies
MIT License
284 stars 56 forks source link

Collisions/concurrency? #46

Closed jezzsantos closed 1 year ago

jezzsantos commented 1 year ago

I am using the Provider once in a CLI (in rolling file configuration)

I am configuring it in code, like this:

.ConfigureLogging((context, logging) =>
                {
                    logging.ClearProviders();
                    logging.AddFile($"logs-{Process.GetCurrentProcess().Id}.log", options =>
                    {
                        options.Append = true;
                        options.MinLevel = LogLevel.Information;
                        options.MaxRollingFiles = 1;
                        options.FileSizeLimitBytes = 10 * 1000 * 1000;
                    });

At runtime, there are several ILogger.Trace() being recorded in the log file correctly, and then I see this exception:

System.IO.IOException: The process cannot access the file 'C:\......\logs-6136.log' because it is being used by another process.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at NReco.Logging.File.FileLoggerProvider.FileWriter.<OpenFile>g__createLogFileStream|7_0(<>c__DisplayClass7_0& )
   at NReco.Logging.File.FileLoggerProvider.FileWriter.OpenFile(Boolean append)
   at NReco.Logging.File.FileLoggerProvider.FileWriter..ctor(FileLoggerProvider fileLogPrv)
   at NReco.Logging.File.FileLoggerProvider..ctor(String fileName, FileLoggerOptions options)
   at Microsoft.Extensions.Logging.FileLoggerExtensions.<>c__DisplayClass1_0.<AddFile>b__0(IServiceProvider srvPrv)

the rest of stack trace is my code, all the way back to a line in the code that tries to make another call to ILogger.Trace()

The last ILogger.Trace() call I make is after shutdown my IHost.

Seems like the LoggingProvider may be keeping the file open all the time? or perhaps we have a writing concurrency issue? Or not correctly closing the file after exiting?

Could that be the case?

jezzsantos commented 1 year ago

Sorry, you can delete this issue.