-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
237 lines (197 loc) · 7.06 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
using Microsoft.AspNet.SignalR.Client;
using log4net.Layout;
namespace Bukimedia.LoggerLibrary
{
enum LogLevel { Debug, Info, Warn, Error, Fatal };
public class Logger
{
string LogFileName { get; set; }
ILog Log { get; set; }
private IHubProxy Hub;
private SignalRAppender _signalRAppender;
private MemoryAppenderWithEvents _memoryAppenderWithEvents;
public Logger(string LogFileName)
{
this.LogFileName = LogFileName;
log4net.GlobalContext.Properties["LogFileName"] = this.LogFileName;
log4net.Config.XmlConfigurator.Configure();
this.Log = LogManager.GetLogger(typeof(Logger));
}
public void SetLogFileName(string LogFileName)
{
this.LogFileName = LogFileName;
log4net.GlobalContext.Properties["LogFileName"] = this.LogFileName;
}
public void SetSignalRHub(IHubProxy hub)
{
this.Hub = hub;
log4net.GlobalContext.Properties["Hub"] = this.Hub;
var layout = new PatternLayout("%utcdate %-5level - %message%newline");
layout.ActivateOptions();
_signalRAppender = new SignalRAppender
{
Layout = layout,
Hub = hub,
};
_signalRAppender.BufferSize = 500;
_signalRAppender.ActivateOptions();
BasicConfigurator.Configure(_signalRAppender);
((log4net.Repository.Hierarchy.Logger)Log.Logger).AddAppender(_signalRAppender);
}
public void SetEventHandler(EventHandler eh)
{
_memoryAppenderWithEvents = new MemoryAppenderWithEvents();
_memoryAppenderWithEvents.Updated += eh;
BasicConfigurator.Configure(_memoryAppenderWithEvents);
((log4net.Repository.Hierarchy.Logger)Log.Logger).AddAppender(_memoryAppenderWithEvents);
}
public void SetAllLogLevel()
{
this.Log.Logger.Repository.Threshold = log4net.Core.Level.All;
}
public void SetDebugLogLevel()
{
this.Log.Logger.Repository.Threshold = log4net.Core.Level.Debug;
}
public void SetInfoLogLevel()
{
this.Log.Logger.Repository.Threshold = log4net.Core.Level.Info;
}
public void SetWarnLogLevel()
{
this.Log.Logger.Repository.Threshold = log4net.Core.Level.Warn;
}
public void SetErrorLogLevel()
{
this.Log.Logger.Repository.Threshold = log4net.Core.Level.Error;
}
public void SetFatalLogLevel()
{
this.Log.Logger.Repository.Threshold = log4net.Core.Level.Fatal;
}
public void SetOffLogLevel()
{
this.Log.Logger.Repository.Threshold = log4net.Core.Level.Off;
}
public void LogDebugStampNoNewline(string Message)
{
this.LogMessage(LogLevel.Debug, true, true, false, Message);
}
public void LogDebugNoStampNewLine(string Message)
{
this.LogMessage(LogLevel.Debug, false, false, true, Message);
}
public void LogDebugStampNewLine(string Message)
{
this.LogMessage(LogLevel.Debug, true, true, true, Message);
}
public void LogInfoStampNoNewLine(string Message)
{
this.LogMessage(LogLevel.Info, true, true, false, Message);
}
public void LogInfoNoStampNewLine(string Message)
{
this.LogMessage(LogLevel.Info, false, false, true, Message);
}
public void LogInfoStampNewLine(string Message)
{
this.LogMessage(LogLevel.Info, true, true, true, Message);
}
public void LogWarnStampNoNewLine(string Message)
{
this.LogMessage(LogLevel.Warn, true, true, false, Message);
}
public void LogWarnNoStampNewLine(string Message)
{
this.LogMessage(LogLevel.Warn, false, false, true, Message);
}
public void LogWarnStampNewLine(string Message)
{
this.LogMessage(LogLevel.Warn, true, true, true, Message);
}
public void LogErrorStampNoNewLine(string Message)
{
this.LogMessage(LogLevel.Error, true, true, false, Message);
}
public void LogErrorNoStampNewLine(string Message)
{
this.LogMessage(LogLevel.Error, false, false, true, Message);
}
public void LogErrorStampNewLine(string Message)
{
this.LogMessage(LogLevel.Error, true, true, true, Message);
}
public void LogFatalStampNoNewLine(string Message)
{
this.LogMessage(LogLevel.Fatal, true, true, false, Message);
}
public void LogFatalNoStampNewLine(string Message)
{
this.LogMessage(LogLevel.Fatal, false, false, true, Message);
}
public void LogFatalStampNewLine(string Message)
{
this.LogMessage(LogLevel.Fatal, true, true, true, Message);
}
private void LogMessage(LogLevel Level, bool ShowStamp, bool ShowLevel, bool ShowNewLine, object Message)
{
string Stamp = "";
string Mode = "";
string NewLine = "";
if (ShowStamp)
{
Stamp = "[" + DateTime.Now.ToString() + "] - ";
}
if (ShowLevel)
{
switch (Level)
{
case LogLevel.Debug:
Mode = "[DEBUG] - ";
break;
case LogLevel.Info:
Mode = "[INFO] - ";
break;
case LogLevel.Warn:
Mode = "[WARN] - ";
break;
case LogLevel.Error:
Mode = "[ERROR] - ";
break;
case LogLevel.Fatal:
Mode = "[FATAL] - ";
break;
}
}
if (ShowNewLine)
{
NewLine = Environment.NewLine;
}
switch (Level)
{
case LogLevel.Debug:
this.Log.Debug(Stamp + Mode + Message + NewLine);
break;
case LogLevel.Info:
this.Log.Info(Stamp + Mode + Message + NewLine);
break;
case LogLevel.Warn:
this.Log.Warn(Stamp + Mode + Message + NewLine);
break;
case LogLevel.Error:
this.Log.Error(Stamp + Mode + Message + NewLine);
break;
case LogLevel.Fatal:
this.Log.Fatal(Stamp + Mode + Message + NewLine);
break;
}
}
}
}