Skip to content

Multiprocess sharing on .NET Core #27

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

Merged
merged 3 commits into from
Nov 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,4 @@ _Pvt_Extensions

# FAKE - F# Make
.fake/
example/Sample/log.txt
6 changes: 3 additions & 3 deletions example/Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public static void Main(string[] args)
{
SelfLog.Enable(Console.Out);

var sw = System.Diagnostics.Stopwatch.StartNew();

Log.Logger = new LoggerConfiguration()
.WriteTo.File("log.txt")
.WriteTo.File("log.txt", shared: true)
Copy link
Member Author

Choose a reason for hiding this comment

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

I'll remove this from the example, since it's possible people will copy it into their projects and sacrifice performance for no reason.

.CreateLogger();

var sw = System.Diagnostics.Stopwatch.StartNew();

for (var i = 0; i < 1000000; ++i)
{
Log.Information("Hello, file logger!");
Expand Down
6 changes: 3 additions & 3 deletions src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static LoggerConfiguration ConfigureFile(

if (shared)
{
#if ATOMIC_APPEND
#if SHARING
if (buffered)
throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));
#else
Expand All @@ -194,7 +194,7 @@ static LoggerConfiguration ConfigureFile(
ILogEventSink sink;
try
{
#if ATOMIC_APPEND
#if SHARING
if (shared)
{
sink = new SharedFileSink(path, formatter, fileSizeLimitBytes);
Expand All @@ -203,7 +203,7 @@ static LoggerConfiguration ConfigureFile(
{
#endif
sink = new FileSink(path, formatter, fileSizeLimitBytes, buffered: buffered);
#if ATOMIC_APPEND
#if SHARING
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public sealed class SharedFileSink : ILogEventSink, IFlushableFileSink, IDisposa
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
/// <exception cref="IOException"></exception>
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes,
Encoding encoding = null)
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (textFormatter == null) throw new ArgumentNullException(nameof(textFormatter));
Expand Down
132 changes: 132 additions & 0 deletions src/Serilog.Sinks.File/Sinks/File/SharedFileSink.OSMutex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright 2013-2016 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if OS_MUTEX

using System;
using System.IO;
using System.Text;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using System.Threading;

namespace Serilog.Sinks.File
{
/// <summary>
/// Write log events to a disk file.
/// </summary>
public sealed class SharedFileSink : ILogEventSink, IFlushableFileSink, IDisposable
{
readonly TextWriter _output;
readonly FileStream _underlyingStream;
readonly ITextFormatter _textFormatter;
readonly long? _fileSizeLimitBytes;
readonly object _syncRoot = new object();

const string MutexNameSuffix = ".serilog";
readonly Mutex _mutex;

/// <summary>Construct a <see cref="FileSink"/>.</summary>
/// <param name="path">Path to the file.</param>
/// <param name="textFormatter">Formatter used to convert log events to text.</param>
/// <param name="fileSizeLimitBytes">The approximate maximum size, in bytes, to which a log file will be allowed to grow.
/// For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit
/// will be written in full even if it exceeds the limit.</param>
/// <param name="encoding">Character encoding used to write the text file. The default is UTF-8 without BOM.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>The file will be written using the UTF-8 character set.</remarks>
/// <exception cref="IOException"></exception>
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (textFormatter == null) throw new ArgumentNullException(nameof(textFormatter));
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0)
throw new ArgumentException("Negative value provided; file size limit must be non-negative");

_textFormatter = textFormatter;
_fileSizeLimitBytes = fileSizeLimitBytes;

var directory = Path.GetDirectoryName(path);
if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

// Backslash is special on Windows
_mutex = new Mutex(false, path.Replace('\\', ':') + MutexNameSuffix);
Copy link

@skomis-mm skomis-mm Nov 24, 2016

Choose a reason for hiding this comment

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

I think its better to name it with the full path name to ensure uniqueness and better for crossplat, something like

Path.GetFullPath(path).Replace(Path.DirectorySeparatorChar, ':') + MutexNameSuffix

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, will do.

_underlyingStream = System.IO.File.Open(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
_output = new StreamWriter(_underlyingStream, encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
}

/// <summary>
/// Emit the provided log event to the sink.
/// </summary>
/// <param name="logEvent">The log event to write.</param>
public void Emit(LogEvent logEvent)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));

lock (_syncRoot)
{
_mutex.WaitOne();
Copy link
Member Author

Choose a reason for hiding this comment

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

On this and the other WaitOne() call below, should we add a timeout and fail if the mutex can't be acquired? E.g. a 10s timeout is highly unlikely to be hit, but would at least keep things alive.

try
{
_underlyingStream.Seek(0, SeekOrigin.End);
if (_fileSizeLimitBytes != null)
{
if (_underlyingStream.Length >= _fileSizeLimitBytes.Value)
return;
}

_textFormatter.Format(logEvent, _output);
_output.Flush();
_underlyingStream.Flush();
}
finally
{
_mutex.ReleaseMutex();
}
}
}

/// <inheritdoc />
public void Dispose()
{
lock (_syncRoot)
{
_output.Dispose();
}
}

/// <inheritdoc />
public void FlushToDisk()
{
lock (_syncRoot)
{
_mutex.WaitOne();
try
{
_underlyingStream.Flush(true);
}
finally
{
_mutex.ReleaseMutex();
}
}
}
}
}

#endif
15 changes: 13 additions & 2 deletions src/Serilog.Sinks.File/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@
},
"frameworks": {
"net4.5": {
"buildOptions": { "define": [ "ATOMIC_APPEND" ] }
"buildOptions": { "define": [ "SHARING", "ATOMIC_APPEND" ] }
},
"netcoreapp1.0": {
"buildOptions": { "define": [ "SHARING", "OS_MUTEX" ] },
"dependencies": {
"System.IO": "4.1.0",
"System.IO.FileSystem": "4.0.1",
"System.IO.FileSystem.Primitives": "4.0.1",
"System.Text.Encoding.Extensions": "4.0.11",
"System.Threading.Timer": "4.0.1",
"System.Threading": "4.0.11"
}
},
"netstandard1.3": {
"dependencies": {
"System.IO": "4.1.0",
"System.IO.FileSystem": "4.0.1",
"System.IO.FileSystem.Primitives": "4.0.1",
"System.Text.Encoding.Extensions": "4.0.11",
"System.Threading.Timer": "4.0.1"
"System.Threading.Timer": "4.0.1"
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions test/Serilog.Sinks.File.Tests/SharedFileSinkTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if ATOMIC_APPEND

using System.IO;
using System.IO;
using Xunit;
using Serilog.Formatting.Json;
using Serilog.Sinks.File.Tests.Support;
Expand Down Expand Up @@ -102,5 +100,3 @@ public void WhenLimitIsNotSpecifiedFileSizeIsNotRestricted()
}
}
}

#endif
3 changes: 0 additions & 3 deletions test/Serilog.Sinks.File.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
]
},
"net4.5.2": {
"buildOptions": {
"define": ["ATOMIC_APPEND"]
}
}
}
}