Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/Exceptionless/Logging/FileExceptionlessLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Exceptionless.Logging {
public class FileExceptionlessLog : IExceptionlessLog, IDisposable {
private static Mutex _flushLock = new Mutex(false, nameof(FileExceptionlessLog));
private static readonly object _flushLock = new object();

private Timer _flushTimer;
private readonly bool _append;
Expand Down Expand Up @@ -132,10 +132,9 @@ public void Flush() {
_isFlushing = true;

Run.WithRetries(() => {
if (!_flushLock.WaitOne(TimeSpan.FromSeconds(5)))
return;
Monitor.TryEnter(_flushLock, TimeSpan.FromSeconds(5), ref hasFlushLock);

hasFlushLock = true;
if (!hasFlushLock) return;

bool append = _append || !_firstWrite;
_firstWrite = false;
Expand All @@ -155,10 +154,9 @@ public void Flush() {
}
});
} catch (Exception ex) {
System.Diagnostics.Trace.WriteLine("Exceptionless: Error flushing log contents to disk: {0}", ex.Message);
System.Diagnostics.Trace.WriteLine("Exceptionless: Error flushing log contents to disk: {0}", ex.ToString());
Comment thread
ejsmith marked this conversation as resolved.
} finally {
if (hasFlushLock)
_flushLock.ReleaseMutex();
if (hasFlushLock) Monitor.Exit(_flushLock);
_isFlushing = false;
}
}
Expand Down Expand Up @@ -215,17 +213,23 @@ internal void CheckFileSize() {

// get the last X lines from the current file
string lastLines = String.Empty;
bool hasFlushLock = false;
try {
Run.WithRetries(() => {
if (!_flushLock.WaitOne(TimeSpan.FromSeconds(5)))
return;
Monitor.TryEnter(_flushLock, TimeSpan.FromSeconds(5), ref hasFlushLock);
if (!hasFlushLock) return;

lastLines = GetLastLinesFromFile(FilePath);

_flushLock.ReleaseMutex();
});
} catch (Exception ex) {
System.Diagnostics.Trace.WriteLine("Exceptionless: Error getting last X lines from the log file: {0}", ex.Message);
}
catch (Exception ex) {
System.Diagnostics.Trace.WriteLine("Exceptionless: Error getting last X lines from the log file: {0}",
ex.Message);
}
finally {
if(hasFlushLock) Monitor.Exit(_flushLock);

}

if (String.IsNullOrEmpty(lastLines)) {
Expand All @@ -234,19 +238,25 @@ internal void CheckFileSize() {
}

// overwrite the log file and initialize it with the last X lines it had
hasFlushLock = false;
try {

Run.WithRetries(() => {
if (!_flushLock.WaitOne(TimeSpan.FromSeconds(5)))
return;
Monitor.TryEnter(_flushLock, TimeSpan.FromSeconds(5), ref hasFlushLock);
if (!hasFlushLock) return;

using (var writer = GetWriter(true))
using (var writer = GetWriter(true))
writer.Value.Write(lastLines);

_flushLock.ReleaseMutex();

});
} catch (Exception ex) {
System.Diagnostics.Trace.WriteLine("Exceptionless: Error rewriting the log file after trimming it: {0}", ex.Message);
}
finally {
if (hasFlushLock) Monitor.Exit(_flushLock);

}

_isCheckingFileSize = false;
}
Expand Down