Skip to content

Commit 06e457a

Browse files
committed
Sharing on netstandard1.3; dispose _mutex; ten second timeout waiting for mutex acquisition
1 parent bce2b62 commit 06e457a

File tree

4 files changed

+21
-29
lines changed

4 files changed

+21
-29
lines changed

example/Sample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void Main(string[] args)
1414
var sw = System.Diagnostics.Stopwatch.StartNew();
1515

1616
Log.Logger = new LoggerConfiguration()
17-
.WriteTo.File("log.txt", shared: true)
17+
.WriteTo.File("log.txt")
1818
.CreateLogger();
1919

2020
for (var i = 0; i < 1000000; ++i)

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,32 +180,20 @@ static LoggerConfiguration ConfigureFile(
180180
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
181181
if (path == null) throw new ArgumentNullException(nameof(path));
182182
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0) throw new ArgumentException("Negative value provided; file size limit must be non-negative");
183-
184-
if (shared)
185-
{
186-
#if SHARING
187-
if (buffered)
188-
throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));
189-
#else
190-
throw new NotSupportedException("File sharing is not supported on this platform.");
191-
#endif
192-
}
183+
if (shared && buffered)
184+
throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));
193185

194186
ILogEventSink sink;
195187
try
196188
{
197-
#if SHARING
198189
if (shared)
199190
{
200191
sink = new SharedFileSink(path, formatter, fileSizeLimitBytes);
201192
}
202193
else
203194
{
204-
#endif
205195
sink = new FileSink(path, formatter, fileSizeLimitBytes, buffered: buffered);
206-
#if SHARING
207196
}
208-
#endif
209197
}
210198
catch (Exception ex)
211199
{

src/Serilog.Sinks.File/Sinks/File/SharedFileSink.OSMutex.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Serilog.Events;
2222
using Serilog.Formatting;
2323
using System.Threading;
24+
using Serilog.Debugging;
2425

2526
namespace Serilog.Sinks.File
2627
{
@@ -36,6 +37,7 @@ public sealed class SharedFileSink : ILogEventSink, IFlushableFileSink, IDisposa
3637
readonly object _syncRoot = new object();
3738

3839
const string MutexNameSuffix = ".serilog";
40+
const int MutexWaitTimeout = 10000;
3941
readonly Mutex _mutex;
4042

4143
/// <summary>Construct a <see cref="FileSink"/>.</summary>
@@ -80,7 +82,12 @@ public void Emit(LogEvent logEvent)
8082

8183
lock (_syncRoot)
8284
{
83-
_mutex.WaitOne();
85+
if (!_mutex.WaitOne(MutexWaitTimeout))
86+
{
87+
SelfLog.WriteLine("Shared file mutex could not be acquired in {0} ms for event emitting", MutexWaitTimeout);
88+
return;
89+
}
90+
8491
try
8592
{
8693
_underlyingStream.Seek(0, SeekOrigin.End);
@@ -107,6 +114,7 @@ public void Dispose()
107114
lock (_syncRoot)
108115
{
109116
_output.Dispose();
117+
_mutex.Dispose();
110118
}
111119
}
112120

@@ -115,7 +123,12 @@ public void FlushToDisk()
115123
{
116124
lock (_syncRoot)
117125
{
118-
_mutex.WaitOne();
126+
if (!_mutex.WaitOne(MutexWaitTimeout))
127+
{
128+
SelfLog.WriteLine("Shared file mutex could not be acquired in {0} ms for disk flush operation", MutexWaitTimeout);
129+
return;
130+
}
131+
119132
try
120133
{
121134
_underlyingStream.Flush(true);

src/Serilog.Sinks.File/project.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
},
1818
"frameworks": {
1919
"net4.5": {
20-
"buildOptions": { "define": [ "SHARING", "ATOMIC_APPEND" ] }
20+
"buildOptions": { "define": [ "ATOMIC_APPEND" ] }
2121
},
22-
"netcoreapp1.0": {
23-
"buildOptions": { "define": [ "SHARING", "OS_MUTEX" ] },
22+
"netstandard1.3": {
23+
"buildOptions": { "define": [ "OS_MUTEX" ] },
2424
"dependencies": {
2525
"System.IO": "4.1.0",
2626
"System.IO.FileSystem": "4.0.1",
@@ -29,15 +29,6 @@
2929
"System.Threading.Timer": "4.0.1",
3030
"System.Threading": "4.0.11"
3131
}
32-
},
33-
"netstandard1.3": {
34-
"dependencies": {
35-
"System.IO": "4.1.0",
36-
"System.IO.FileSystem": "4.0.1",
37-
"System.IO.FileSystem.Primitives": "4.0.1",
38-
"System.Text.Encoding.Extensions": "4.0.11",
39-
"System.Threading.Timer": "4.0.1"
40-
}
4132
}
4233
}
4334
}

0 commit comments

Comments
 (0)