3
3
using System . IO ;
4
4
using System . Text ;
5
5
using System . Threading ;
6
- using Exceptionless . Storage ;
7
6
using Exceptionless . Utility ;
8
7
9
8
namespace Exceptionless . Logging {
10
9
public class FileExceptionlessLog : IExceptionlessLog , IDisposable {
10
+ private static Mutex _flushLock = new Mutex ( false , nameof ( FileExceptionlessLog ) ) ;
11
+
11
12
private Timer _flushTimer ;
12
13
private readonly bool _append ;
13
14
private bool _firstWrite = true ;
@@ -126,34 +127,40 @@ public void Flush() {
126
127
if ( _isFlushing || _buffer . IsEmpty )
127
128
return ;
128
129
130
+ bool hasFlushLock = false ;
129
131
try {
130
132
_isFlushing = true ;
131
133
132
134
Run . WithRetries ( ( ) => {
133
- using ( var logFileLock = LockFile . Acquire ( FilePath + ".lock" , TimeSpan . FromMilliseconds ( 500 ) ) ) {
134
- bool append = _append || ! _firstWrite ;
135
- _firstWrite = false ;
136
-
137
- try {
138
- using ( var writer = GetWriter ( append ) ) {
139
- LogEntry entry ;
140
- while ( _buffer . TryDequeue ( out entry ) ) {
141
- if ( entry != null && entry . LogLevel >= MinimumLogLevel )
142
- writer . Value . WriteLine ( $ "{ FormatLongDate ( entry . Timestamp ) } { entry . LogLevel . ToString ( ) . PadRight ( 5 ) } { entry . Message } ") ;
143
- }
144
- }
145
- } catch ( Exception ex ) {
146
- System . Diagnostics . Trace . TraceError ( "Unable flush the logs. " + ex . Message ) ;
135
+ if ( ! _flushLock . WaitOne ( TimeSpan . FromSeconds ( 5 ) ) )
136
+ return ;
137
+
138
+ hasFlushLock = true ;
139
+
140
+ bool append = _append || ! _firstWrite ;
141
+ _firstWrite = false ;
142
+
143
+ try {
144
+ using ( var writer = GetWriter ( append ) ) {
147
145
LogEntry entry ;
148
146
while ( _buffer . TryDequeue ( out entry ) ) {
149
- System . Diagnostics . Trace . WriteLine ( entry ) ;
147
+ if ( entry != null && entry . LogLevel >= MinimumLogLevel )
148
+ writer . Value . WriteLine ( $ "{ FormatLongDate ( entry . Timestamp ) } { entry . LogLevel . ToString ( ) . PadRight ( 5 ) } { entry . Message } ") ;
150
149
}
151
150
}
151
+ } catch ( Exception ex ) {
152
+ System . Diagnostics . Trace . TraceError ( "Unable flush the logs. " + ex . Message ) ;
153
+ LogEntry entry ;
154
+ while ( _buffer . TryDequeue ( out entry ) ) {
155
+ System . Diagnostics . Trace . WriteLine ( entry ) ;
156
+ }
152
157
}
153
158
} ) ;
154
159
} catch ( Exception ex ) {
155
160
System . Diagnostics . Trace . WriteLine ( "Exceptionless: Error flushing log contents to disk: {0}" , ex . Message ) ;
156
161
} finally {
162
+ if ( hasFlushLock )
163
+ _flushLock . ReleaseMutex ( ) ;
157
164
_isFlushing = false ;
158
165
}
159
166
}
@@ -212,11 +219,12 @@ internal void CheckFileSize() {
212
219
string lastLines = String . Empty ;
213
220
try {
214
221
Run . WithRetries ( ( ) => {
215
- using ( var logFileLock = LockFile . Acquire ( FilePath + ".lock" , TimeSpan . FromMilliseconds ( 500 ) ) ) {
216
- try {
217
- lastLines = GetLastLinesFromFile ( FilePath ) ;
218
- } catch { }
219
- }
222
+ if ( ! _flushLock . WaitOne ( TimeSpan . FromSeconds ( 5 ) ) )
223
+ return ;
224
+
225
+ lastLines = GetLastLinesFromFile ( FilePath ) ;
226
+
227
+ _flushLock . ReleaseMutex ( ) ;
220
228
} ) ;
221
229
} catch ( Exception ex ) {
222
230
System . Diagnostics . Trace . WriteLine ( "Exceptionless: Error getting last X lines from the log file: {0}" , ex . Message ) ;
@@ -230,10 +238,13 @@ internal void CheckFileSize() {
230
238
// overwrite the log file and initialize it with the last X lines it had
231
239
try {
232
240
Run . WithRetries ( ( ) => {
233
- using ( var logFileLock = LockFile . Acquire ( FilePath + ".lock" , TimeSpan . FromMilliseconds ( 500 ) ) ) {
234
- using ( var writer = GetWriter ( true ) )
235
- writer . Value . Write ( lastLines ) ;
236
- }
241
+ if ( ! _flushLock . WaitOne ( TimeSpan . FromSeconds ( 5 ) ) )
242
+ return ;
243
+
244
+ using ( var writer = GetWriter ( true ) )
245
+ writer . Value . Write ( lastLines ) ;
246
+
247
+ _flushLock . ReleaseMutex ( ) ;
237
248
} ) ;
238
249
} catch ( Exception ex ) {
239
250
System . Diagnostics . Trace . WriteLine ( "Exceptionless: Error rewriting the log file after trimming it: {0}" , ex . Message ) ;
0 commit comments