Skip to content

Commit

Permalink
Merge pull request #167 from edwardmeng/master
Browse files Browse the repository at this point in the history
Add event time and level for the file logger.
  • Loading branch information
niemyjski authored Nov 6, 2017
2 parents ad4063b + 865f6cd commit 7324de5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 33 additions & 1 deletion src/Exceptionless/Logging/FileExceptionlessLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void Flush() {
LogEntry entry;
while (_buffer.TryDequeue(out entry)) {
if (entry != null && entry.LogLevel >= MinimumLogLevel)
writer.Value.WriteLine(entry.Message);
writer.Value.WriteLine($"{FormatLongDate(entry.Timestamp)} {entry.LogLevel.ToString().PadRight(5)} {entry.Message}");
}
}
} catch (Exception ex) {
Expand All @@ -154,6 +154,36 @@ public void Flush() {
}
}

private string FormatLongDate(DateTime timestamp) {
var builder = new StringBuilder();
Append4DigitsZeroPadded(timestamp.Year);
builder.Append('-');
Append2DigitsZeroPadded(timestamp.Month);
builder.Append('-');
Append2DigitsZeroPadded(timestamp.Day);
builder.Append(' ');
Append2DigitsZeroPadded(timestamp.Hour);
builder.Append(':');
Append2DigitsZeroPadded(timestamp.Minute);
builder.Append(':');
Append2DigitsZeroPadded(timestamp.Second);
builder.Append('.');
Append4DigitsZeroPadded((int)(timestamp.Ticks % 10000000) / 1000);
return builder.ToString();

void Append4DigitsZeroPadded(int number) {
builder.Append((char)(number / 1000 % 10 + 0x30));
builder.Append((char)(number / 100 % 10 + 0x30));
builder.Append((char)(number / 10 % 10 + 0x30));
builder.Append((char)(number / 1 % 10 + 0x30));
}

void Append2DigitsZeroPadded(int number) {
builder.Append((char)(number / 10 + 0x30));
builder.Append((char)(number % 10 + 0x30));
}
}

private readonly ConcurrentQueue<LogEntry> _buffer = new ConcurrentQueue<LogEntry>();
private void WriteEntry(LogLevel level, string entry) {
_buffer.Enqueue(new LogEntry(level, entry));
Expand Down Expand Up @@ -249,8 +279,10 @@ private class LogEntry {
public LogEntry(LogLevel level, string message) {
LogLevel = level;
Message = message;
Timestamp = DateTime.Now;
}

public DateTime Timestamp { get; set; }
public LogLevel LogLevel { get; set; }
public string Message { get; set; }
}
Expand Down
4 changes: 2 additions & 2 deletions test/Exceptionless.Tests/Log/FileExceptionlessLogTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public virtual void CanWriteToLogFile() {
Assert.True(LogExists());
string contents = log.GetFileContents();

Assert.Equal("Test\r\n", contents);
Assert.EndsWith(" Info Test\r\n", contents);
}
}

Expand All @@ -36,7 +36,7 @@ public virtual void LogFlushTimerWorks() {
Assert.True(LogExists());
contents = log.GetFileContents();

Assert.Equal("Test\r\n", contents);
Assert.EndsWith(" Info Test\r\n", contents);
}
}

Expand Down

0 comments on commit 7324de5

Please sign in to comment.