@@ -66,8 +66,8 @@ public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeL
66
66
Directory . CreateDirectory ( directory ) ;
67
67
}
68
68
69
- // Backslash is special on Windows
70
- _mutex = new Mutex ( false , path . Replace ( ' \\ ' , ':' ) + MutexNameSuffix ) ;
69
+ var mutexName = Path . GetFullPath ( path ) . Replace ( Path . DirectorySeparatorChar , ':' ) + MutexNameSuffix ;
70
+ _mutex = new Mutex ( false , mutexName ) ;
71
71
_underlyingStream = System . IO . File . Open ( path , FileMode . Append , FileAccess . Write , FileShare . ReadWrite ) ;
72
72
_output = new StreamWriter ( _underlyingStream , encoding ?? new UTF8Encoding ( encoderShouldEmitUTF8Identifier : false ) ) ;
73
73
}
@@ -82,11 +82,8 @@ public void Emit(LogEvent logEvent)
82
82
83
83
lock ( _syncRoot )
84
84
{
85
- if ( ! _mutex . WaitOne ( MutexWaitTimeout ) )
86
- {
87
- SelfLog . WriteLine ( "Shared file mutex could not be acquired in {0} ms for event emitting" , MutexWaitTimeout ) ;
85
+ if ( ! TryAcquireMutex ( ) )
88
86
return ;
89
- }
90
87
91
88
try
92
89
{
@@ -103,7 +100,7 @@ public void Emit(LogEvent logEvent)
103
100
}
104
101
finally
105
102
{
106
- _mutex . ReleaseMutex ( ) ;
103
+ ReleaseMutex ( ) ;
107
104
}
108
105
}
109
106
}
@@ -123,21 +120,41 @@ public void FlushToDisk()
123
120
{
124
121
lock ( _syncRoot )
125
122
{
126
- if ( ! _mutex . WaitOne ( MutexWaitTimeout ) )
127
- {
128
- SelfLog . WriteLine ( "Shared file mutex could not be acquired in {0} ms for disk flush operation" , MutexWaitTimeout ) ;
123
+ if ( ! TryAcquireMutex ( ) )
129
124
return ;
130
- }
131
125
132
126
try
133
127
{
134
128
_underlyingStream . Flush ( true ) ;
135
129
}
136
130
finally
137
131
{
138
- _mutex . ReleaseMutex ( ) ;
132
+ ReleaseMutex ( ) ;
133
+ }
134
+ }
135
+ }
136
+
137
+ bool TryAcquireMutex ( )
138
+ {
139
+ try
140
+ {
141
+ if ( ! _mutex . WaitOne ( MutexWaitTimeout ) )
142
+ {
143
+ SelfLog . WriteLine ( "Shared file mutex could not be acquired within {0} ms" , MutexWaitTimeout ) ;
144
+ return false ;
139
145
}
140
146
}
147
+ catch ( AbandonedMutexException )
148
+ {
149
+ SelfLog . WriteLine ( "Inherited shared file mutex after abandonment by another process" ) ;
150
+ }
151
+
152
+ return true ;
153
+ }
154
+
155
+ void ReleaseMutex ( )
156
+ {
157
+ _mutex . ReleaseMutex ( ) ;
141
158
}
142
159
}
143
160
}
0 commit comments