1
- #if ! PORTABLE && ! NETSTANDARD1_2
1
+ #if ! PORTABLE && ! NETSTANDARD1_2
2
2
using System ;
3
3
using System . Collections . Concurrent ;
4
4
using System . IO ;
@@ -13,6 +13,7 @@ public class FileExceptionlessLog : IExceptionlessLog, IDisposable {
13
13
private readonly bool _append ;
14
14
private bool _firstWrite = true ;
15
15
private bool _isFlushing = false ;
16
+ private bool _isCheckingFileSize = false ;
16
17
17
18
public FileExceptionlessLog ( string filePath , bool append = false ) {
18
19
if ( String . IsNullOrEmpty ( filePath ) )
@@ -66,7 +67,7 @@ protected internal virtual long GetFileSize() {
66
67
try {
67
68
if ( File . Exists ( FilePath ) )
68
69
return new FileInfo ( FilePath ) . Length ;
69
- } catch ( IOException ex ) {
70
+ } catch ( Exception ex ) {
70
71
System . Diagnostics . Trace . WriteLine ( "Exceptionless: Error getting size of file: {0}" , ex . Message ) ;
71
72
}
72
73
@@ -119,9 +120,12 @@ public void Flush() {
119
120
if ( _isFlushing || _buffer . Count == 0 )
120
121
return ;
121
122
122
- if ( DateTime . Now . Subtract ( _lastSizeCheck ) . TotalSeconds > 120 )
123
+ if ( DateTime . UtcNow . Subtract ( _lastSizeCheck ) . TotalSeconds > 120 )
123
124
CheckFileSize ( ) ;
124
125
126
+ if ( _isFlushing )
127
+ return ;
128
+
125
129
try {
126
130
_isFlushing = true ;
127
131
@@ -189,14 +193,20 @@ private void WriteEntry(LogLevel level, string entry) {
189
193
_buffer . Enqueue ( new LogEntry ( level , entry ) ) ;
190
194
}
191
195
192
- private DateTime _lastSizeCheck = DateTime . Now ;
196
+ private DateTime _lastSizeCheck = DateTime . UtcNow ;
193
197
protected const long FIVE_MB = 5 * 1024 * 1024 ;
194
198
195
199
internal void CheckFileSize ( ) {
196
- _lastSizeCheck = DateTime . Now ;
200
+ if ( _isCheckingFileSize )
201
+ return ;
197
202
198
- if ( GetFileSize ( ) <= FIVE_MB )
203
+ _isCheckingFileSize = true ;
204
+ _lastSizeCheck = DateTime . UtcNow ;
205
+
206
+ if ( GetFileSize ( ) <= FIVE_MB ) {
207
+ _isCheckingFileSize = false ;
199
208
return ;
209
+ }
200
210
201
211
// get the last X lines from the current file
202
212
string lastLines = String . Empty ;
@@ -212,8 +222,10 @@ internal void CheckFileSize() {
212
222
System . Diagnostics . Trace . WriteLine ( "Exceptionless: Error getting last X lines from the log file: {0}" , ex . Message ) ;
213
223
}
214
224
215
- if ( String . IsNullOrEmpty ( lastLines ) )
225
+ if ( String . IsNullOrEmpty ( lastLines ) ) {
226
+ _isCheckingFileSize = false ;
216
227
return ;
228
+ }
217
229
218
230
// overwrite the log file and initialize it with the last X lines it had
219
231
try {
@@ -226,6 +238,8 @@ internal void CheckFileSize() {
226
238
} catch ( Exception ex ) {
227
239
System . Diagnostics . Trace . WriteLine ( "Exceptionless: Error rewriting the log file after trimming it: {0}" , ex . Message ) ;
228
240
}
241
+
242
+ _isCheckingFileSize = false ;
229
243
}
230
244
231
245
private void OnFlushTimer ( object state ) {
0 commit comments