-
Notifications
You must be signed in to change notification settings - Fork 33
Support multi-process shared file sets #16
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -39,11 +39,12 @@ public sealed class RollingFileSink : ILogEventSink, IDisposable | |
readonly int? _retainedFileCountLimit; | ||
readonly Encoding _encoding; | ||
readonly bool _buffered; | ||
readonly bool _shared; | ||
readonly object _syncRoot = new object(); | ||
|
||
bool _isDisposed; | ||
DateTime? _nextCheckpoint; | ||
FileSink _currentFile; | ||
ILogEventSink _currentFile; | ||
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. Ideally the two file sinks will expose a common interface/ABC; for now |
||
|
||
/// <summary>Construct a <see cref="RollingFileSink"/>.</summary> | ||
/// <param name="pathFormat">String describing the location of the log files, | ||
|
@@ -54,28 +55,36 @@ public sealed class RollingFileSink : ILogEventSink, IDisposable | |
/// For unrestricted growth, pass null. The default is 1 GB.</param> | ||
/// <param name="retainedFileCountLimit">The maximum number of log files that will be retained, | ||
/// including the current log file. For unlimited retention, pass null. The default is 31.</param> | ||
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8.</param> | ||
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param> | ||
/// <param name="buffered">Indicates if flushing to the output file can be buffered or not. The default | ||
/// is false.</param> | ||
/// <param name="shared">Allow the log files to be shared by multiple processes. The default is false.</param> | ||
/// <returns>Configuration object allowing method chaining.</returns> | ||
/// <remarks>The file will be written using the UTF-8 character set.</remarks> | ||
public RollingFileSink(string pathFormat, | ||
ITextFormatter textFormatter, | ||
long? fileSizeLimitBytes, | ||
int? retainedFileCountLimit, | ||
Encoding encoding = null, | ||
bool buffered = false) | ||
bool buffered = false, | ||
bool shared = false) | ||
{ | ||
if (pathFormat == null) throw new ArgumentNullException(nameof(pathFormat)); | ||
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative"); | ||
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1"); | ||
|
||
#if !SHARING | ||
if (shared) | ||
throw new NotSupportedException("File sharing is not supported on this platform."); | ||
#endif | ||
|
||
_roller = new TemplatedPathRoller(pathFormat); | ||
_textFormatter = textFormatter; | ||
_fileSizeLimitBytes = fileSizeLimitBytes; | ||
_retainedFileCountLimit = retainedFileCountLimit; | ||
_encoding = encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); | ||
_encoding = encoding; | ||
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. DRY: Serilog.Sinks.File configures the default encoding, including it here means the two may drift if one is updated without updating the other. |
||
_buffered = buffered; | ||
_shared = shared; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -98,8 +107,7 @@ public void Emit(LogEvent logEvent) | |
// If the file was unable to be opened on the last attempt, it will remain | ||
// null until the next checkpoint passes, at which time another attempt will be made to | ||
// open it. | ||
if (_currentFile != null) | ||
_currentFile.Emit(logEvent); | ||
_currentFile?.Emit(logEvent); | ||
} | ||
} | ||
|
||
|
@@ -148,7 +156,13 @@ void OpenFile(DateTime now) | |
|
||
try | ||
{ | ||
#if SHARING | ||
_currentFile = _shared ? | ||
(ILogEventSink)new SharedFileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding) : | ||
new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered); | ||
#else | ||
_currentFile = new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered); | ||
#endif | ||
} | ||
catch (IOException ex) | ||
{ | ||
|
@@ -223,7 +237,7 @@ void CloseFile() | |
{ | ||
if (_currentFile != null) | ||
{ | ||
_currentFile.Dispose(); | ||
(_currentFile as IDisposable)?.Dispose(); | ||
_currentFile = null; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "2.3.0-*", | ||
"version": "3.0.0-*", | ||
"description": "The rolling file sink for Serilog - Simple .NET logging with fully-structured events", | ||
"authors": [ "Serilog Contributors" ], | ||
"packOptions": { | ||
|
@@ -9,16 +9,17 @@ | |
"iconUrl": "http://serilog.net/images/serilog-sink-nuget.png" | ||
}, | ||
"dependencies": { | ||
"Serilog": "2.0.0", | ||
"Serilog.Sinks.File": "2.0.0" | ||
"Serilog.Sinks.File": "3.0.0-dev-00735" | ||
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. Maybe make a note to switch this back to 3.0 when this goes 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.
|
||
}, | ||
"buildOptions": { | ||
"keyFile": "../../assets/Serilog.snk", | ||
"xmlDoc": true, | ||
"warningsAsErrors": true | ||
}, | ||
"frameworks": { | ||
"net4.5": {}, | ||
"net4.5": { | ||
"buildOptions": {"define": ["SHARING"]} | ||
}, | ||
"netstandard1.3": { | ||
"dependencies": { | ||
"System.IO": "4.1.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace Serilog.Tests | ||
{ | ||
public class RollingFileLoggerConfigurationExtensionsTests | ||
{ | ||
[Fact] | ||
public void BuffferingIsNotAvailableWhenSharingEnabled() | ||
{ | ||
Assert.Throws<ArgumentException>(() => | ||
new LoggerConfiguration() | ||
.WriteTo.RollingFile("logs", buffered: true, shared: true)); | ||
} | ||
} | ||
} | ||
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. Tests are admittedly a bit light, it's fair to assume the |
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.
if(shared && buffered)
?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.
Oops .. copy-pasta, thanks :-)