This repository was archived by the owner on Oct 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
194 lines (173 loc) · 5.15 KB
/
Logger.cs
File metadata and controls
194 lines (173 loc) · 5.15 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
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
using System;
namespace Xevle.Logging
{
/// <summary>
/// Generic logger with support for multiple output modules.
/// </summary>
public class Logger : IDisposable
{
/// <summary>
/// The current log level.
/// </summary>
LogLevel logLevel = LogLevel.CriticalToInformation;
/// <summary>
/// Gets or sets the log level.
/// </summary>
/// <value>The log level.</value>
public LogLevel LogLevel {
get {
return logLevel;
}
set {
logLevel = value & LogLevel.CriticalToVerbose;
}
}
/// <summary>
/// The internal event handler.
/// </summary>
EventHandler<LogEventArgs> internalEventHandler;
/// <summary>
/// The internal event handler lock.
/// </summary>
readonly object internalEventHandlerLock = new object();
/// <summary>
/// Initializes a new instance of the <see cref="T:Xevle.Logging.Logger"/> class.
/// </summary>
/// <param name="logLevel">Log level.</param>
public Logger(LogLevel logLevel = LogLevel.CriticalToInformation)
{
LogLevel = logLevel;
}
/// <summary>
/// Adds the output.
/// </summary>
/// <param name="logger">Logger.</param>
public void AddOutput(ILogger logger)
{
lock (internalEventHandlerLock) {
internalEventHandler += logger.Recieve;
}
}
/// <summary>
/// Log the specified logLevel, message and args.
/// </summary>
/// <param name="logLevel">Log level.</param>
/// <param name="message">Message.</param>
/// <param name="args">Arguments.</param>
public void Log(LogLevel logLevel, string message, params object [] args)
{
if ((logLevel & LogLevel.DebugAndMessageBox) != 0) {
#if !DEBUG
return;
#else
if ((logLevel & LogLevel.CriticalToVerbose) == 0) logLevel |= LogLevel.Information;
#endif
}
if ((logLevel & LogLevel) == 0) return; // Ignore message, log level of message is to low.
string text = message;
if (args.Length > 0)
{
text = string.Format(message, args);
}
Fire(this, new LogEventArgs(logLevel, text));
}
/// <summary>
/// Log the specified message and args as fatal log event.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="args">Arguments.</param>
public void Fatal(String message, params object [] args)
{
Log(LogLevel.Fatal, message, args);
}
/// <summary>
/// Log the specified message and args as eror log event.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="args">Arguments.</param>
public void Error(String message, params object [] args)
{
Log(LogLevel.Error, message, args);
}
/// <summary>
/// Log the specified message and args as fatal log event.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="args">Arguments.</param>
public void Warning(String message, params object [] args)
{
Log(LogLevel.Warning, message, args);
}
/// <summary>
/// Log the specified message and args as information log event.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="args">Arguments.</param>
public void Information(String message, params object [] args)
{
Log(LogLevel.Information, message, args);
}
/// <summary>
/// Log the specified message and args as verbose log event.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="args">Arguments.</param>
public void Verbose(String message, params object [] args)
{
Log(LogLevel.Verbose, message, args);
}
/// <summary>
/// Log the specified message and args as debug log event.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="args">Arguments.</param>
public void Debug(String message, params object [] args)
{
Log(LogLevel.Debug, message, args);
}
/// <summary>
/// Log the specified message and args as trace log event.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="args">Arguments.</param>
public void Trace(String message, params object [] args)
{
Log(LogLevel.Trace, message, args);
}
/// <summary>
/// Fire the specified sender and e.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">E.</param>
void Fire(object sender, LogEventArgs e)
{
EventHandler<LogEventArgs> handler;
lock (internalEventHandlerLock) {
handler = internalEventHandler;
}
if (handler != null) {
handler(sender, e);
}
}
/// <summary>
/// Removes all outputs.
/// </summary>
public void RemoveAllOutputs()
{
lock (internalEventHandlerLock) {
internalEventHandler = null;
}
}
/// <summary>
/// Releases all resource used by the <see cref="T:Xevle.Logging.Logger"/> object.
/// </summary>
/// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="T:Xevle.Logging.Logger"/>. The
/// <see cref="Dispose"/> method leaves the <see cref="T:Xevle.Logging.Logger"/> in an unusable state. After calling
/// <see cref="Dispose"/>, you must release all references to the <see cref="T:Xevle.Logging.Logger"/> so the garbage
/// collector can reclaim the memory that the <see cref="T:Xevle.Logging.Logger"/> was occupying.</remarks>
public void Dispose()
{
RemoveAllOutputs();
}
}
}