Repository navigation
Expand file tree
/
Copy pathLogger.cs
More file actions
73 lines (63 loc) · 2.47 KB
/
Copy pathLogger.cs
File metadata and controls
73 lines (63 loc) · 2.47 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
namespace WindBot
{
public static class Logger
{
private static readonly object _syncRoot = new object();
// Each server-mode bot is driven by its own worker thread, so its log context must not be shared.
[ThreadStatic]
private static Func<string> _contextProvider;
public static void SetContext(Func<string> contextProvider)
{
_contextProvider = contextProvider;
}
public static void ClearContext()
{
_contextProvider = null;
}
public static void WriteLine(string message)
{
lock (_syncRoot)
Console.WriteLine("[" + DateTime.Now.ToString("yy-MM-dd HH:mm:ss") + "] " + message);
}
public static void DebugWriteLine(string message, bool printStackTrace = false)
{
#if DEBUG
lock (_syncRoot)
{
Console.WriteLine("[" + DateTime.Now.ToString("yy-MM-dd HH:mm:ss") + "] " + message);
if (printStackTrace)
Console.WriteLine(new System.Diagnostics.StackTrace(1, true).ToString());
}
#endif
}
public static void WriteErrorLine(string message)
{
WriteErrorLine(message, null, new System.Diagnostics.StackTrace(1, true).ToString());
}
public static void WriteErrorLine(string message, Exception exception)
{
WriteErrorLine(message, exception, null);
}
private static void WriteErrorLine(string message, Exception exception, string callStack)
{
lock (_syncRoot)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine("[" + DateTime.Now.ToString("yy-MM-dd HH:mm:ss") + "] " + message);
Console.ResetColor();
string context = _contextProvider?.Invoke();
if (!string.IsNullOrEmpty(context))
Console.Error.WriteLine("Context: " + context);
if (exception != null)
Console.Error.WriteLine(exception);
else if (!string.IsNullOrEmpty(callStack))
{
Console.Error.WriteLine("Call stack:");
Console.Error.WriteLine(callStack);
}
}
}
}
}