-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to delete log files and recreate them immediately #144
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,9 @@ namespace Serilog.Sinks.File | |
[Obsolete("This type will be removed from the public API in a future version; use `WriteTo.File(shared: true)` instead.")] | ||
public sealed class SharedFileSink : IFileSink, IDisposable | ||
{ | ||
readonly TextWriter _output; | ||
readonly FileStream _underlyingStream; | ||
TextWriter _output; | ||
FileStream _underlyingStream; | ||
readonly string _path; | ||
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed to make |
||
readonly ITextFormatter _textFormatter; | ||
readonly long? _fileSizeLimitBytes; | ||
readonly object _syncRoot = new object(); | ||
|
@@ -52,21 +53,21 @@ public sealed class SharedFileSink : IFileSink, IDisposable | |
/// <exception cref="IOException"></exception> | ||
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null) | ||
{ | ||
if (path == null) throw new ArgumentNullException(nameof(path)); | ||
_path = path ?? throw new ArgumentNullException(nameof(path)); | ||
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) | ||
throw new ArgumentException("Negative value provided; file size limit must be non-negative"); | ||
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter)); | ||
_fileSizeLimitBytes = fileSizeLimitBytes; | ||
|
||
var directory = Path.GetDirectoryName(path); | ||
var directory = Path.GetDirectoryName(_path); | ||
if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory)) | ||
{ | ||
Directory.CreateDirectory(directory); | ||
} | ||
|
||
var mutexName = Path.GetFullPath(path).Replace(Path.DirectorySeparatorChar, ':') + MutexNameSuffix; | ||
var mutexName = Path.GetFullPath(_path).Replace(Path.DirectorySeparatorChar, ':') + MutexNameSuffix; | ||
_mutex = new Mutex(false, mutexName); | ||
_underlyingStream = System.IO.File.Open(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); | ||
_underlyingStream = System.IO.File.Open(_path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); | ||
_output = new StreamWriter(_underlyingStream, encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); | ||
} | ||
|
||
|
@@ -81,6 +82,13 @@ bool IFileSink.EmitOrOverflow(LogEvent logEvent) | |
|
||
try | ||
{ | ||
if (!System.IO.File.Exists(_path)) | ||
{ | ||
_underlyingStream.Dispose(); | ||
_underlyingStream = System.IO.File.Open(_path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); | ||
_output = new StreamWriter(_underlyingStream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); | ||
} | ||
Comment on lines
+85
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although I'm worried about the performance of this |
||
|
||
_underlyingStream.Seek(0, SeekOrigin.End); | ||
if (_fileSizeLimitBytes != null) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File deletion was enabled by adding this, but I don't know if it's the best way to do this.
Should we make another config entry to only enable deletion when required?