Skip to content

Commit 7324de5

Browse files
authored
Merge pull request #167 from edwardmeng/master
Add event time and level for the file logger.
2 parents ad4063b + 865f6cd commit 7324de5

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/Exceptionless/Logging/FileExceptionlessLog.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void Flush() {
135135
LogEntry entry;
136136
while (_buffer.TryDequeue(out entry)) {
137137
if (entry != null && entry.LogLevel >= MinimumLogLevel)
138-
writer.Value.WriteLine(entry.Message);
138+
writer.Value.WriteLine($"{FormatLongDate(entry.Timestamp)} {entry.LogLevel.ToString().PadRight(5)} {entry.Message}");
139139
}
140140
}
141141
} catch (Exception ex) {
@@ -154,6 +154,36 @@ public void Flush() {
154154
}
155155
}
156156

157+
private string FormatLongDate(DateTime timestamp) {
158+
var builder = new StringBuilder();
159+
Append4DigitsZeroPadded(timestamp.Year);
160+
builder.Append('-');
161+
Append2DigitsZeroPadded(timestamp.Month);
162+
builder.Append('-');
163+
Append2DigitsZeroPadded(timestamp.Day);
164+
builder.Append(' ');
165+
Append2DigitsZeroPadded(timestamp.Hour);
166+
builder.Append(':');
167+
Append2DigitsZeroPadded(timestamp.Minute);
168+
builder.Append(':');
169+
Append2DigitsZeroPadded(timestamp.Second);
170+
builder.Append('.');
171+
Append4DigitsZeroPadded((int)(timestamp.Ticks % 10000000) / 1000);
172+
return builder.ToString();
173+
174+
void Append4DigitsZeroPadded(int number) {
175+
builder.Append((char)(number / 1000 % 10 + 0x30));
176+
builder.Append((char)(number / 100 % 10 + 0x30));
177+
builder.Append((char)(number / 10 % 10 + 0x30));
178+
builder.Append((char)(number / 1 % 10 + 0x30));
179+
}
180+
181+
void Append2DigitsZeroPadded(int number) {
182+
builder.Append((char)(number / 10 + 0x30));
183+
builder.Append((char)(number % 10 + 0x30));
184+
}
185+
}
186+
157187
private readonly ConcurrentQueue<LogEntry> _buffer = new ConcurrentQueue<LogEntry>();
158188
private void WriteEntry(LogLevel level, string entry) {
159189
_buffer.Enqueue(new LogEntry(level, entry));
@@ -249,8 +279,10 @@ private class LogEntry {
249279
public LogEntry(LogLevel level, string message) {
250280
LogLevel = level;
251281
Message = message;
282+
Timestamp = DateTime.Now;
252283
}
253284

285+
public DateTime Timestamp { get; set; }
254286
public LogLevel LogLevel { get; set; }
255287
public string Message { get; set; }
256288
}

test/Exceptionless.Tests/Log/FileExceptionlessLogTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public virtual void CanWriteToLogFile() {
1818
Assert.True(LogExists());
1919
string contents = log.GetFileContents();
2020

21-
Assert.Equal("Test\r\n", contents);
21+
Assert.EndsWith(" Info Test\r\n", contents);
2222
}
2323
}
2424

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

39-
Assert.Equal("Test\r\n", contents);
39+
Assert.EndsWith(" Info Test\r\n", contents);
4040
}
4141
}
4242

0 commit comments

Comments
 (0)