-
Notifications
You must be signed in to change notification settings - Fork 5
/
Logger.cs
118 lines (110 loc) · 2.74 KB
/
Logger.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace WinService
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
internal enum LogLevel : byte
{
All = 0,
Trace = 1,
Debug = 2,
Info = 3,
Warn = 4,
Error = 5,
Fatal = 6
}
internal static class Logger
{
private static Object consoleLockObj = new Object();
private static Object fileLockObj = new Object();
private static LogLevel CurrentLevel = LogLevel.Info;
private static String logFile = null;
static Logger()
{
String appLogLevel = System.Configuration.ConfigurationManager.AppSettings["SelfLogLevel"];
CurrentLevel = (LogLevel)Enum.Parse(typeof(LogLevel), appLogLevel);
logFile = System.Configuration.ConfigurationManager.AppSettings["SelfLogFile"];
if (!String.IsNullOrEmpty(logFile))
{
logFile = Environment.ExpandEnvironmentVariables(logFile);
}
}
private static Boolean CanLog(LogLevel level)
{
return level >= CurrentLevel;
}
private static ConsoleColor getColor(LogLevel level)
{
ConsoleColor ans = Console.ForegroundColor;
switch (level)
{
case LogLevel.All:
ans = ConsoleColor.White;
break;
case LogLevel.Trace:
ans = ConsoleColor.Green;
break;
case LogLevel.Debug:
ans = ConsoleColor.Yellow;
break;
case LogLevel.Info:
ans = ConsoleColor.Gray;
break;
case LogLevel.Warn:
ans = ConsoleColor.Cyan;
break;
case LogLevel.Error:
ans = ConsoleColor.Red;
break;
case LogLevel.Fatal:
ans = ConsoleColor.Magenta;
break;
default:
break;
}
return ans;
}
internal static void WriteMessage(LogLevel level, String msg)
{
if (CanLog(level))
{
if (Environment.UserInteractive)
{
lock (consoleLockObj)
{
ConsoleColor currentColor = Console.ForegroundColor;
Console.ForegroundColor = getColor(level);
Console.WriteLine("{0} - {1}", DateTime.Now, msg);
Console.ForegroundColor = currentColor;
}
}
if (!String.IsNullOrEmpty(logFile))
{
lock (fileLockObj)
{
//TODO: Roll log (max size / daily)
System.IO.File.AppendAllText(logFile, String.Format("{0} - {1}{2}", DateTime.Now, msg, Environment.NewLine), Encoding.UTF8);
}
}
}
}
internal static void WriteMessage(String msg)
{
WriteMessage(LogLevel.Info, msg);
}
internal static void WriteMessage(LogLevel level, String format, params String[] args)
{
WriteMessage(level, String.Format(format, args));
}
internal static void WriteMessage(String format, params String[] args)
{
WriteMessage(String.Format(format, args));
}
internal static void WriteException(Exception e, String msg)
{
WriteMessage(LogLevel.Error, "{0} - {1} - {2}", msg, e.Message, e.StackTrace);
}
}
}