jirkapenzes / SimpleLogger

Simple Logger C#.NET
MIT License
94 stars 42 forks source link

System.IO.IOException: 'The process cannot access the file #13

Open chatay opened 3 years ago

chatay commented 3 years ago

whilst using publish method in FileLoggerHandler with many threads/services, it throws the error cannot access the file as it is not locked. If you agree too, I will fork and modify the code to let it work simultaneously.

 public void Publish(LogMessage logMessage)
        {
            if (!string.IsNullOrEmpty(_directory))
            {
                var directoryInfo = new DirectoryInfo(Path.Combine(_directory));
                if (!directoryInfo.Exists)
                    directoryInfo.Create();
            }
            using (var writer = new StreamWriter(File.Open(Path.Combine(_directory, _fileName), FileMode.Append)))
                writer.WriteLine(_loggerFormatter.ApplyFormat(logMessage));

        }
infiniteloopltd commented 2 years ago

+1 - I am seeing the same issue. If the file is locked, then the logger shouldn't throw an exception.

Pull Request?