From f51fbc18f442bcd0b63b632729ccca60afbca804 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 26 Aug 2016 16:46:07 +1000 Subject: [PATCH 1/4] Dev version bump [Skip CI] --- src/Serilog.Sinks.File/project.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.Sinks.File/project.json b/src/Serilog.Sinks.File/project.json index 4c47d61..95f9137 100644 --- a/src/Serilog.Sinks.File/project.json +++ b/src/Serilog.Sinks.File/project.json @@ -1,5 +1,5 @@ { - "version": "3.0.0-*", + "version": "3.0.1-*", "description": "Write Serilog events to a text file in plain or JSON format.", "authors": [ "Serilog Contributors" ], "packOptions": { From 7893956d1aae6e831059f75ab47739f3a4eb260e Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 26 Aug 2016 16:49:00 +1000 Subject: [PATCH 2/4] Added notes on sharing [Skip CI] --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5aa395f..267b97d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ To avoid bringing down apps with runaway disk usage the file sink **limits file .WriteTo.File("log.txt", fileSizeLimitBytes: null) ``` -> **Important:** Only one process may write to a log file at a given time. For multi-process scenarios, either use separate files or one of the non-file-based sinks. +> **Important:** By default only one process may use a log file at a given time. See _Shared log files_ below if multi-process logging is required. ### `` configuration @@ -36,6 +36,14 @@ To emit JSON, rather than plain text, a formatter can be specified: To configure an alternative formatter in XML ``, specify the formatter's assembly-qualified type name as the setting `value`. +### Shared log files + +Multiple processes can concurrently write to the same log file if the `shared` parameter is set to `true`: + +```csharp + .WriteTo.File("log.txt", shared: true) +``` + ### Performance By default, the file sink will flush each event written through it to disk. To improve write performance, specifying `buffered: true` will permit the underlying stream to buffer writes. From 6e6d4f50b45d02afbfeb7a0b733827e77489a5a1 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 26 Aug 2016 16:51:51 +1000 Subject: [PATCH 3/4] Get rid of an unreachable code warning (.NET Core) --- .../FileLoggerConfigurationExtensions.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs b/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs index 0942cb3..8cffba0 100644 --- a/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs +++ b/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs @@ -178,12 +178,12 @@ static LoggerConfiguration ConfigureFile( if (shared) { -#if !ATOMIC_APPEND - throw new NotSupportedException("File sharing is not supported on this platform."); -#endif - +#if ATOMIC_APPEND if (buffered) throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered)); +#else + throw new NotSupportedException("File sharing is not supported on this platform."); +#endif } ILogEventSink sink; @@ -215,4 +215,4 @@ static LoggerConfiguration ConfigureFile( return addSink(sink, restrictedToMinimumLevel, levelSwitch); } } -} \ No newline at end of file +} From 916696b3a71ce6fbfc60eefdeaaffbce86c80a54 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 31 Aug 2016 07:26:51 +1000 Subject: [PATCH 4/4] Fixes serilog-sinks-rollingfile#20 - shared log file not readable when in use. --- src/Serilog.Sinks.File/Sinks/File/FileSink.cs | 9 --------- src/Serilog.Sinks.File/Sinks/File/SharedFileSink.cs | 4 ++-- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/Serilog.Sinks.File/Sinks/File/FileSink.cs b/src/Serilog.Sinks.File/Sinks/File/FileSink.cs index fe81144..3af8386 100644 --- a/src/Serilog.Sinks.File/Sinks/File/FileSink.cs +++ b/src/Serilog.Sinks.File/Sinks/File/FileSink.cs @@ -14,9 +14,6 @@ using System; using System.IO; -#if ATOMIC_APPEND -using System.Security.AccessControl; -#endif using System.Text; using Serilog.Core; using Serilog.Events; @@ -64,13 +61,7 @@ public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBy Directory.CreateDirectory(directory); } -#if ATOMIC_APPEND - // FileSystemRights.AppendData improves performance substantially (~30%) when available. - Stream file = new FileStream(path, FileMode.Append, FileSystemRights.AppendData, FileShare.Read, 4096, FileOptions.None); -#else Stream file = System.IO.File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read); -#endif - if (_fileSizeLimitBytes != null) { file = _countingStreamWrapper = new WriteCountingStream(file); diff --git a/src/Serilog.Sinks.File/Sinks/File/SharedFileSink.cs b/src/Serilog.Sinks.File/Sinks/File/SharedFileSink.cs index 02ecad3..a8af612 100644 --- a/src/Serilog.Sinks.File/Sinks/File/SharedFileSink.cs +++ b/src/Serilog.Sinks.File/Sinks/File/SharedFileSink.cs @@ -75,7 +75,7 @@ public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeL path, FileMode.Append, FileSystemRights.AppendData, - FileShare.Write, + FileShare.ReadWrite, _fileStreamBufferLength, FileOptions.None); @@ -118,7 +118,7 @@ public void Emit(LogEvent logEvent) _path, FileMode.Append, FileSystemRights.AppendData, - FileShare.Write, + FileShare.ReadWrite, length, FileOptions.None); _fileStreamBufferLength = length;