Skip to content

Commit f07646c

Browse files
niemyjskiejsmith
authored andcommitted
Added some reentry checks to try and prevent overlapping io calls.
1 parent 178ae44 commit f07646c

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/Exceptionless/Logging/FileExceptionlessLog.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !PORTABLE && !NETSTANDARD1_2
1+
#if !PORTABLE && !NETSTANDARD1_2
22
using System;
33
using System.Collections.Concurrent;
44
using System.IO;
@@ -13,6 +13,7 @@ public class FileExceptionlessLog : IExceptionlessLog, IDisposable {
1313
private readonly bool _append;
1414
private bool _firstWrite = true;
1515
private bool _isFlushing = false;
16+
private bool _isCheckingFileSize = false;
1617

1718
public FileExceptionlessLog(string filePath, bool append = false) {
1819
if (String.IsNullOrEmpty(filePath))
@@ -66,7 +67,7 @@ protected internal virtual long GetFileSize() {
6667
try {
6768
if (File.Exists(FilePath))
6869
return new FileInfo(FilePath).Length;
69-
} catch (IOException ex) {
70+
} catch (Exception ex) {
7071
System.Diagnostics.Trace.WriteLine("Exceptionless: Error getting size of file: {0}", ex.Message);
7172
}
7273

@@ -119,9 +120,12 @@ public void Flush() {
119120
if (_isFlushing || _buffer.Count == 0)
120121
return;
121122

122-
if (DateTime.Now.Subtract(_lastSizeCheck).TotalSeconds > 120)
123+
if (DateTime.UtcNow.Subtract(_lastSizeCheck).TotalSeconds > 120)
123124
CheckFileSize();
124125

126+
if (_isFlushing)
127+
return;
128+
125129
try {
126130
_isFlushing = true;
127131

@@ -189,14 +193,20 @@ private void WriteEntry(LogLevel level, string entry) {
189193
_buffer.Enqueue(new LogEntry(level, entry));
190194
}
191195

192-
private DateTime _lastSizeCheck = DateTime.Now;
196+
private DateTime _lastSizeCheck = DateTime.UtcNow;
193197
protected const long FIVE_MB = 5 * 1024 * 1024;
194198

195199
internal void CheckFileSize() {
196-
_lastSizeCheck = DateTime.Now;
200+
if (_isCheckingFileSize)
201+
return;
197202

198-
if (GetFileSize() <= FIVE_MB)
203+
_isCheckingFileSize = true;
204+
_lastSizeCheck = DateTime.UtcNow;
205+
206+
if (GetFileSize() <= FIVE_MB) {
207+
_isCheckingFileSize = false;
199208
return;
209+
}
200210

201211
// get the last X lines from the current file
202212
string lastLines = String.Empty;
@@ -212,8 +222,10 @@ internal void CheckFileSize() {
212222
System.Diagnostics.Trace.WriteLine("Exceptionless: Error getting last X lines from the log file: {0}", ex.Message);
213223
}
214224

215-
if (String.IsNullOrEmpty(lastLines))
225+
if (String.IsNullOrEmpty(lastLines)) {
226+
_isCheckingFileSize = false;
216227
return;
228+
}
217229

218230
// overwrite the log file and initialize it with the last X lines it had
219231
try {
@@ -226,6 +238,8 @@ internal void CheckFileSize() {
226238
} catch (Exception ex) {
227239
System.Diagnostics.Trace.WriteLine("Exceptionless: Error rewriting the log file after trimming it: {0}", ex.Message);
228240
}
241+
242+
_isCheckingFileSize = false;
229243
}
230244

231245
private void OnFlushTimer(object state) {

0 commit comments

Comments
 (0)