Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Serilog.Sinks.File/Sinks/File/SharedFileSink.AtomicAppend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeL
path,
FileMode.Append,
FileSystemRights.AppendData,
FileShare.ReadWrite,
FileShare.ReadWrite | FileShare.Delete,
Copy link
Contributor Author

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?

_fileStreamBufferLength,
FileOptions.None);

Expand Down Expand Up @@ -101,7 +101,23 @@ bool IFileSink.EmitOrOverflow(LogEvent logEvent)
_path,
FileMode.Append,
FileSystemRights.AppendData,
FileShare.ReadWrite,
FileShare.ReadWrite | FileShare.Delete,
length,
FileOptions.None);
_fileStreamBufferLength = length;

oldOutput.Dispose();
}

if (!System.IO.File.Exists(_path))
{
var oldOutput = _fileOutput;

_fileOutput = new FileStream(
_path,
FileMode.Append,
FileSystemRights.AppendData,
FileShare.ReadWrite | FileShare.Delete,
length,
FileOptions.None);
_fileStreamBufferLength = length;
Expand Down
20 changes: 14 additions & 6 deletions src/Serilog.Sinks.File/Sinks/File/SharedFileSink.OSMutex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to make _output and _underlyingStream non-readonly, so I could dispose them and recreate the file after it was deleted.
Also added _path variable to be re-used when recreating the file.

readonly ITextFormatter _textFormatter;
readonly long? _fileSizeLimitBytes;
readonly object _syncRoot = new object();
Expand All @@ -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));
}

Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I'm worried about the performance of this File.Exists(), this was how I managed to make the file be re-created. If you guys have another idea of how to make this verification, please let me know.
I also noticed that this chunk must be before _underlyingStream.Seek(0, SeekOrigin.End); in order to create the file correctly after is deleted.


_underlyingStream.Seek(0, SeekOrigin.End);
if (_fileSizeLimitBytes != null)
{
Expand Down
32 changes: 32 additions & 0 deletions test/Serilog.Sinks.File.Tests/FileSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,38 @@ public void FileIsWrittenIfNonexistent()
Assert.Contains("Hello, world!", lines[0]);
}
}
[Fact]
public void FileIsReWrittenAfterEventIfDeleted()
{
using (var tmp = TempFolder.ForCaller())
{
var nonexistent = tmp.AllocateFilename("txt");
var evt = Some.LogEvent("Hello, world!");

void Emmit()
{
using (var sink = new FileSink(nonexistent, new JsonFormatter(), null))
{
sink.Emit(evt);
}
}

Emmit();
var lines = System.IO.File.ReadAllLines(nonexistent);
Assert.Contains("Hello, world!", lines[0]);
Assert.Single(lines);

System.IO.File.Delete(nonexistent);
Assert.False(System.IO.File.Exists(nonexistent));
Assert.Throws<FileNotFoundException>(() => System.IO.File.ReadAllLines(nonexistent));

Emmit();
lines = System.IO.File.ReadAllLines(nonexistent);
Assert.True(System.IO.File.Exists(nonexistent));
Assert.Contains("Hello, world!", lines[0]);
Assert.Single(lines);
}
}

[Fact]
public void FileIsAppendedToWhenAlreadyCreated()
Expand Down
68 changes: 68 additions & 0 deletions test/Serilog.Sinks.File.Tests/RollingFileSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Threading;
using Xunit;
using Serilog.Events;
using Serilog.Sinks.File.Tests.Support;
Expand Down Expand Up @@ -261,6 +262,73 @@ public void IfTheLogFolderDoesNotExistItWillBeCreated()
}
}

[Fact]
public void ShouldReCreateDeletedFiles()
{
var fileName = Some.String() + "-{Date}.txt";
var temp = Some.TempFolderPath();
var folder = Path.Combine(temp, Guid.NewGuid().ToString());
var pathFormat = Path.Combine(folder, fileName);

Logger log = null;

try
{
log = new LoggerConfiguration()
.WriteTo.File(pathFormat, rollingInterval: RollingInterval.Day, shared:true)
.CreateLogger();

log.Write(Some.InformationEvent());
Assert.True(Directory.Exists(folder));

var createdFile = Directory.GetFiles(folder)[0];
System.IO.File.Delete(createdFile);
Assert.False(System.IO.File.Exists(createdFile));

log = new LoggerConfiguration()
.WriteTo.File(pathFormat, rollingInterval: RollingInterval.Day, shared: true)
.CreateLogger();

log.Write(Some.InformationEvent());
Assert.True(System.IO.File.Exists(createdFile));
}
finally
{
log?.Dispose();
Directory.Delete(temp, true);
}
}

[Fact]
public void ShouldBeAbleToDeleteFile()
{
var fileName = Some.String() + "-{Date}.txt";
var temp = Some.TempFolderPath();
var folder = Path.Combine(temp, Guid.NewGuid().ToString());
var pathFormat = Path.Combine(folder, fileName);

Logger log = null;

try
{
log = new LoggerConfiguration()
.WriteTo.File(pathFormat, retainedFileCountLimit: 3, rollingInterval: RollingInterval.Day, shared:true, flushToDiskInterval:TimeSpan.FromSeconds(1), buffered:false)
.CreateLogger();

log.Write(Some.InformationEvent());
Assert.True(Directory.Exists(folder));

var createdFile = Directory.GetFiles(folder)[0];
System.IO.File.Delete(createdFile);
Assert.False(System.IO.File.Exists(createdFile));
}
finally
{
log?.Dispose();
Directory.Delete(temp, true);
}
}

[Fact]
public void AssemblyVersionIsFixedAt200()
{
Expand Down
32 changes: 32 additions & 0 deletions test/Serilog.Sinks.File.Tests/SharedFileSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ public void FileIsWrittenIfNonexistent()
Assert.Contains("Hello, world!", lines[0]);
}
}
[Fact]
public void FileIsReWrittenAfterEventIfDeleted()
{
using (var tmp = TempFolder.ForCaller())
{
var nonexistent = tmp.AllocateFilename("txt");
var evt = Some.LogEvent("Hello, world!");

void Emmit()
{
using (var sink = new SharedFileSink(nonexistent, new JsonFormatter(), null))
{
sink.Emit(evt);
}
}

Emmit();
var lines = System.IO.File.ReadAllLines(nonexistent);
Assert.Contains("Hello, world!", lines[0]);
Assert.Single(lines);

System.IO.File.Delete(nonexistent);
Assert.False(System.IO.File.Exists(nonexistent));
Assert.Throws<FileNotFoundException>(() => System.IO.File.ReadAllLines(nonexistent));

Emmit();
lines = System.IO.File.ReadAllLines(nonexistent);
Assert.True(System.IO.File.Exists(nonexistent));
Assert.Contains("Hello, world!", lines[0]);
Assert.Single(lines);
}
}

[Fact]
public void FileIsAppendedToWhenAlreadyCreated()
Expand Down