diff --git a/EonaCat.Logger/EonaCat.Logger.csproj b/EonaCat.Logger/EonaCat.Logger.csproj index 52effbe..cc5db48 100644 --- a/EonaCat.Logger/EonaCat.Logger.csproj +++ b/EonaCat.Logger/EonaCat.Logger.csproj @@ -13,8 +13,8 @@ EonaCat (Jeroen Saey) EonaCat;Logger;EonaCatLogger;Log;Writer;Jeroen;Saey - 1.7.7 - 1.7.7 + 1.7.8 + 1.7.8 README.md True LICENSE diff --git a/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs b/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs index 992d0ed..2dce145 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs @@ -361,6 +361,17 @@ public sealed class FileLoggerProvider : BatchingLoggerProvider, IDisposable { try { + if (_stream == null || !File.Exists(_filePath)) + { + RecreateStream(); + } + + _stream.Write(data, 0, length); + } + catch (IOException ioEx) when (!File.Exists(_filePath)) + { + // File was removed while writing, recreate and retry once + RecreateStream(); _stream.Write(data, 0, length); } catch (Exception ex) @@ -378,15 +389,31 @@ public sealed class FileLoggerProvider : BatchingLoggerProvider, IDisposable } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void FlushIfNeeded() + private void RecreateStream() { - if (_position >= FlushThreshold) + try { - FlushInternal(); + Directory.CreateDirectory(Path.GetDirectoryName(_filePath)!); + + _stream?.Dispose(); + _stream = new FileStream( + _filePath, + FileMode.Append, + FileAccess.Write, + FileShare.ReadWrite | FileShare.Delete, + 4096, + FileOptions.WriteThrough | FileOptions.SequentialScan); + + _size = _stream.Length; + } + catch (Exception ex) + { + RaiseError(ex); + _running = false; } } + private void FlushInternal() { if (_position == 0) @@ -396,6 +423,16 @@ public sealed class FileLoggerProvider : BatchingLoggerProvider, IDisposable try { + if (_stream == null || !File.Exists(_filePath)) + { + RecreateStream(); + } + + _stream.Write(_buffer, 0, _position); + } + catch (IOException ioEx) when (!File.Exists(_filePath)) + { + RecreateStream(); _stream.Write(_buffer, 0, _position); } catch (Exception ex)