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

Multiprocess sharing on .NET Core #27

Merged
merged 3 commits into from
Nov 27, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
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
4 changes: 2 additions & 2 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")
.CreateLogger();

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

for (var i = 0; i < 1000000; ++i)
{
Log.Information("Hello, file logger!");
Expand Down
16 changes: 2 additions & 14 deletions src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,32 +180,20 @@ static LoggerConfiguration ConfigureFile(
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
if (path == null) throw new ArgumentNullException(nameof(path));
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative");

if (shared)
{
#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
}
if (shared && buffered)
throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));

ILogEventSink sink;
try
{
#if ATOMIC_APPEND
if (shared)
{
sink = new SharedFileSink(path, formatter, fileSizeLimitBytes);
}
else
{
#endif
sink = new FileSink(path, formatter, fileSizeLimitBytes, buffered: buffered);
#if ATOMIC_APPEND
}
#endif
}
catch (Exception ex)
{
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
145 changes: 145 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,145 @@
// 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;
using Serilog.Debugging;

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";
const int MutexWaitTimeout = 10000;
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)
{
if (!_mutex.WaitOne(MutexWaitTimeout))
{
SelfLog.WriteLine("Shared file mutex could not be acquired in {0} ms for event emitting", MutexWaitTimeout);
return;
}

Choose a reason for hiding this comment

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

I believe you should also handle AbandonedMutexException in case of another process crash and also protect against async exceptions with help of return value of WaitOne():

bool ownsMutex = false;
try
{
    try
    {
        ownsMutex = _syncRoot.WaitOne(MutexWaitTimeout);
    }
    catch (System.Threading.AbandonedMutexException)
    {
        ownsMutex = true;
    }

    // ...
}
finally
 {
      if (ownsMutex)
      {
          _syncRoot.ReleaseMutex();
      }
 }

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I'll look into it 👍

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();
_mutex.Dispose();
}
}

/// <inheritdoc />
public void FlushToDisk()
{
lock (_syncRoot)
{
if (!_mutex.WaitOne(MutexWaitTimeout))
{
SelfLog.WriteLine("Shared file mutex could not be acquired in {0} ms for disk flush operation", MutexWaitTimeout);
return;
}

try
{
_underlyingStream.Flush(true);
}
finally
{
_mutex.ReleaseMutex();
}
}
}
}
}

#endif
4 changes: 3 additions & 1 deletion src/Serilog.Sinks.File/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"buildOptions": { "define": [ "ATOMIC_APPEND" ] }
},
"netstandard1.3": {
"buildOptions": { "define": [ "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.Timer": "4.0.1",
"System.Threading": "4.0.11"
}
}
}
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"]
}
}
}
}