-
Notifications
You must be signed in to change notification settings - Fork 139
/
TraceLogger.cs
44 lines (38 loc) · 1.5 KB
/
TraceLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Diagnostics;
namespace PnP.Framework.Diagnostics
{
class TraceLogger : ILogger
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "OfficeDevPnP.Core.Diagnostics.LogEntry.set_Message(System.String)")]
public void Debug(LogEntry entry)
{
entry.Message = entry.Message;
Trace.TraceInformation(GetLogEntry(entry, LogLevel.Debug));
}
public void Error(LogEntry entry)
{
Trace.TraceError(GetLogEntry(entry, LogLevel.Error));
}
public void Info(LogEntry entry)
{
Trace.TraceInformation(GetLogEntry(entry, LogLevel.Information));
}
public void Warning(LogEntry entry)
{
Trace.TraceWarning(GetLogEntry(entry, LogLevel.Information));
}
private static string GetLogEntry(LogEntry entry, LogLevel level)
{
try
{
string log = string.Format("{0}\t[{1}]\t[{2}]\t[{3}]\t{4}\t{5}ms\t{6}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), entry.Source, entry.ThreadId, level.ToString(), entry.Message, entry.EllapsedMilliseconds, entry.CorrelationId != Guid.Empty ? entry.CorrelationId.ToString() : "");
return log;
}
catch (Exception e)
{
return $"Error while generating log information, {e}";
}
}
}
}