-
Notifications
You must be signed in to change notification settings - Fork 117
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
Changes from 2 commits
bce2b62
06e457a
b023427
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 |
---|---|---|
|
@@ -234,3 +234,4 @@ _Pvt_Extensions | |
|
||
# FAKE - F# Make | ||
.fake/ | ||
example/Sample/log.txt |
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); | ||
_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; | ||
} | ||
|
||
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. I believe you should also handle bool ownsMutex = false;
try
{
try
{
ownsMutex = _syncRoot.WaitOne(MutexWaitTimeout);
}
catch (System.Threading.AbandonedMutexException)
{
ownsMutex = true;
}
// ...
}
finally
{
if (ownsMutex)
{
_syncRoot.ReleaseMutex();
}
} 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. 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,6 @@ | |
] | ||
}, | ||
"net4.5.2": { | ||
"buildOptions": { | ||
"define": ["ATOMIC_APPEND"] | ||
} | ||
} | ||
} | ||
} |
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.
I think its better to name it with the full path name to ensure uniqueness and better for crossplat, something like
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.
Good point, will do.