-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cs
472 lines (423 loc) · 13.1 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace NetLog.Logging
{
public delegate void LogDetailsListener(Logger log);
public class Logger
{
private String name;
private List<Handler> handlers;
internal volatile Level level;
private static bool consoleDebug;
private volatile bool useParentHandlers = true;
private List<LogDetailsListener>listeners = new List<LogDetailsListener>();
public bool UseParentHandlers {
get{ return useParentHandlers; }
set { useParentHandlers = value; }
}
/// <summary>
/// Add a detail listener. Such listeners are called when the log level
/// changes for this instance, or a handler is added or removed. This
/// allows some extra behavior of the logger to be controlled. For interaction
/// with other logging/event services, the level change notification can be
/// used to update some external data values so that logging levels can
/// be adjusted appropriately there as well.
/// </summary>
/// <param name="lis"></param>
/// <returns>true if added, false if already a listener</returns>
public bool AddDetailListener( LogDetailsListener lis ) {
if( listeners.Contains(lis) == false ) {
listeners.Add(lis);
NotifyDetailListener(lis);
return true;
}
return false;
}
/// <summary>
/// Remove a detail listener
/// </summary>
/// <param name="lis"></param>
/// <returns>true if removed, false if not a registered listener</returns>
public bool RemoveDetailListener( LogDetailsListener lis ) {
if( listeners.Contains(lis) ) {
listeners.Remove(lis);
return true;
}
return false;
}
public static bool ConsoleDebug
{
get { return consoleDebug; }
set { consoleDebug = value; }
}
public static Logger GetLogger ( object obj ) {
return GetLogger( obj.GetType().FullName );
}
internal static Logger NeedNewLogger( string name) {
Logger l;
// Have to do this up front because it may create logging instances
// to preset levels when logging.properties has such content, and so
// we want to find those logger instances, instead of creating them here,
// and then not seeing those instances if we do things out of order.
LogManager lm = LogManager.GetLogManager( );
if ( consoleDebug )
Console.WriteLine( "Get logger \"" + name + "\": have? " + ( LogManager.Loggers.ContainsKey( name ) ? ( "YES, Level: " + LogManager.Loggers[name].Level ) : "NO" ) );
// lock to make sure we only insert one instance
lock( LogManager.Loggers ) {
if( LogManager.Loggers.ContainsKey( name ) == false ) {
l = new Logger( name );
} else {
l = LogManager.Loggers[name];
}
}
return l;
}
/// <summary>
/// Get the logger instance associated with the passed name. Typically name will
/// be passed from a class level field initialization something like:
///
/// private static Logger log = Logger.GetLogger(typeof(ThisClassesName).FullName);
///
/// The use of FullName will allow the software to create a hierarchial name
/// space that can be easily controlled to turn various classes and subclasses and
/// innerclasses logging on and off as needed.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static Logger GetLogger( String name ) {
Logger l;
// Have to do this up front because it may create logging instances
// to preset levels when logging.properties has such content, and so
// we want to find those logger instances, instead of creating them here,
// and then not seeing those instances if we do things out of order.
LogManager lm = LogManager.GetLogManager( );
if ( consoleDebug )
Console.WriteLine( "Get logger \"" + name + "\": have? " + ( LogManager.Loggers.ContainsKey( name ) ? ( "YES, Level: " + LogManager.Loggers[name].Level ) : "NO" ) );
lock( LogManager.Loggers ) {
if( LogManager.Loggers.ContainsKey( name ) == false ) {
l = new Logger( name );
if( name.Equals("") ) {
Handler h = new ConsoleHandler();
l.handlers.Add( h );
// This is the only level that we force to .INFO. All
// others will find there level here, or explicitly set.
l.Level = Level.INFO;
if( h.Formatter == null )
h.Formatter = new StreamFormatter();
}
lm.AddLogger( l );
} else {
l = LogManager.Loggers[name];
}
}
return l;
}
/// <summary>
/// Flush all handlers associated with this logger
/// </summary>
public void Flush() {
Logger logger = this;
while ( logger != null ) {
if ( consoleDebug )
Console.WriteLine( "Flushing \"" + Name + "\" handlers: " + handlers.Count );
foreach ( Handler h in logger.GetHandlers( ) ) {
h.Flush();
}
if ( UseParentHandlers == false )
break;
logger = logger.Parent;
}
}
public Level Level {
set {
if( value.IntValue == Level.ALL.IntValue ) {
Console.WriteLine("All Level Set For logger=" + name);
}
this.level = value;
if ( consoleDebug )
Console.WriteLine( Name + " level now " + value );
NotifyDetailListeners();
}
get {
Level l = this.level;
return l == null ?
(LogManager.GetLogManager().LevelOfLogger(name)) :
l;
}
}
private void NotifyDetailListeners() {
foreach( LogDetailsListener l in listeners ) {
NotifyDetailListener(l);
}
}
private void NotifyDetailListener( LogDetailsListener l ) {
try {
l(this);
} catch( Exception ex ) {
LogManager.ReportExceptionToEventLog(string.Format(
"Error in LogDetailsListener: {0}: {1}", l, ex.Message), ex);
}
}
public Logger Parent {
get {
if( name.Equals("") )
return null;
string[] arr = this.name.Split(new char[]{ '.' } );
string ln = "";
for( int i = 0; i < arr.Count()-1; ++i ) {
if( ln.Length == 0 ) {
ln = arr[i];
} else {
ln = ln + "." + arr[i];
}
}
return Logger.GetLogger(ln);
}
}
public string Name {
set { this.name = value; }
get { return this.name; }
}
public void AddHandler( Handler h ) {
handlers.Add(h );
NotifyDetailListeners();
}
public void RemoveHandler( Handler h ) {
handlers.Remove( h );
try {
h.Close();
} catch( Exception ex ) {
LogManager.ReportExceptionToEventLog( ex.Message, ex );
}
NotifyDetailListeners();
}
public List<Handler> GetHandlers() {
return handlers;
}
/// <summary>
/// WARNING: Only use this constructor for private Logger instances which you wish to not be visible
/// for remote administration.
/// </summary>
/// <param name="name"></param>
public Logger( string name ) {
this.name = name;
this.handlers = new List<Handler>();
}
public void finest ( Exception ex ) {
log( Level.FINEST, ex );
}
public void finest ( string msg ) {
log( Level.FINEST, msg );
}
public void finest ( string msg, Exception ex ) {
log( Level.FINEST, msg, ex );
}
public void finest ( string msg, Exception ex, params object[ ] parms ) {
log( Level.FINEST, msg, ex, parms );
}
public void finest ( string msg, params object[] parms ) {
log( Level.FINEST, msg, parms );
}
public void finer ( Exception ex ) {
log( Level.FINER, ex );
}
public void finer ( string msg ) {
log( Level.FINER, msg );
}
public void finer ( string msg, Exception ex ) {
log( Level.FINER, msg, ex );
}
public void finer ( string msg, Exception ex, params object[ ] parms ) {
log( Level.FINER, msg, ex, parms );
}
public void finer ( string msg, params object[ ] parms ) {
log( Level.FINER, msg, parms );
}
public void entering( Type sourceClass, object sourceMethod ) {
entering(sourceClass.FullName, sourceMethod.ToString());
}
public void entering( Type sourceClass, object sourceMethod, params object[] param ) {
entering(sourceClass.FullName, sourceMethod.ToString(), param);
}
public void entering( string sourceClass, string sourceMethod ) {
LogRecord rec = new LogRecord(Level.FINER, "ENTRY");
rec.SourceClassName = sourceClass;
rec.SourceMethodName = sourceMethod;
log(rec);
}
public void entering(string sourceClass, string sourceMethod, params object[] param)
{
LogRecord rec = new LogRecord(Level.FINER, "ENTRY");
rec.SourceClassName = sourceClass;
rec.SourceMethodName = sourceMethod;
rec.Parameters = param;
log(rec);
}
public void exiting(string sourceClass, string sourceMethod)
{
LogRecord rec = new LogRecord(Level.FINER, "EXIT");
rec.SourceClassName = sourceClass;
rec.SourceMethodName = sourceMethod;
log(rec);
}
public void exiting(string sourceClass, string sourceMethod, params object[] param)
{
LogRecord rec = new LogRecord(Level.FINER, "EXIT");
rec.SourceClassName = sourceClass;
rec.SourceMethodName = sourceMethod;
rec.Parameters = param;
log(rec);
}
public void throwing(string sourceClass, string sourceMethod, Exception thrown)
{
LogRecord rec = new LogRecord(Level.FINER, "THROW");
rec.SourceClassName = sourceClass;
rec.SourceMethodName = sourceMethod;
rec.Thrown = thrown;
log(rec);
}
public void fine ( Exception ex ) {
log( Level.FINE, ex );
}
public void fine ( string msg ) {
log( Level.FINE, msg );
}
public void fine( string msg, Exception ex ) {
log(Level.FINE, msg, ex);
}
public void fine( string msg, params object[] parms ) {
log(Level.FINE, msg, parms);
}
public void fine( string msg, Exception ex, params object[] parms ) {
log( Level.FINE, msg, ex, parms );
}
public void config ( Exception ex ) {
log( Level.CONFIG, ex );
}
public void config ( string msg ) {
log( Level.CONFIG, msg );
}
public void config( string msg, Exception ex ) {
log(Level.CONFIG, msg, ex);
}
public void config ( string msg, Exception ex, params object[ ] parms ) {
log( Level.CONFIG, msg, ex, parms );
}
public void config ( string msg, params object[ ] parms ) {
log( Level.CONFIG, msg, parms );
}
public void info ( Exception ex ) {
log( Level.INFO, ex );
}
public void info ( string msg ) {
log( Level.INFO, msg );
}
public void info ( string msg, Exception ex ) {
log( Level.INFO, msg, ex );
}
public void info ( string msg, Exception ex, params object[ ] parms ) {
log( Level.INFO, msg, ex, parms );
}
public void info ( string msg, params object[ ] parms ) {
log( Level.INFO, msg, parms );
}
public void warning ( Exception ex ) {
log( Level.WARNING, ex );
}
public void warning ( string msg ) {
log( Level.WARNING, msg );
}
public void warning ( string msg, Exception ex ) {
log( Level.WARNING, msg, ex );
}
public void warning ( string msg, Exception ex, params object[ ] parms ) {
log( Level.WARNING, msg, ex, parms );
}
public void warning ( string msg, params object[ ] parms ) {
log( Level.WARNING, msg, parms );
}
public void severe ( Exception ex ) {
log( Level.SEVERE, ex );
}
public void severe ( string msg ) {
log( Level.SEVERE, msg );
}
public void severe ( string msg, Exception ex ) {
log( Level.SEVERE, msg, ex );
}
public void severe ( string msg, Exception ex, params object[ ] parms ) {
log( Level.SEVERE, msg, ex, parms );
}
public void severe ( string msg, params object[ ] parms ) {
log( Level.SEVERE, msg, parms );
}
public void log ( Level level, Exception ex ) {
LogRecord rec = new LogRecord( level, ex.GetType().FullName+": "+ex.Message );
rec.Thrown = ex;
log( rec );
}
public void log ( Level level, string msg )
{
log( new LogRecord( level, msg ) );
}
public void log ( Level level, string msg, Exception ex ) {
LogRecord rec = new LogRecord( level, msg );
rec.Thrown = ex;
log( rec );
}
public void log ( Level level, string msg, Exception ex, params object[ ] parms ) {
LogRecord rec = new LogRecord( level, msg );
rec.Parameters = parms;
rec.Thrown = ex;
log( rec );
}
public void log ( Level level, string msg, params object[ ] parms )
{
LogRecord rec = new LogRecord( level, msg );
rec.Parameters = parms;
log (rec) ;
}
/// <summary>
///
/// </summary>
/// <param name="level"></param>
/// <returns></returns>
[Obsolete]
public bool isLoggable( Level level ) {
return IsLoggable(level);
}
public bool IsLoggable( Level level ) {
return ( level.IntValue >= this.Level.IntValue && this.Level != Level.OFF );
}
public void log( LogRecord rec )
{
rec.LoggerName = name;
Logger logger = this;
Level lowest = logger.Level;
while( logger != null ) {
if( consoleDebug )
Console.WriteLine("\"" + logger.Name + "\" handlers: " + logger.handlers.Count);
if( consoleDebug )
Console.WriteLine("record level " + rec.Level + ", logger level: " + logger.Level + ", logging");
if( rec.Level.IntValue < lowest.IntValue || lowest == Level.OFF ) {
return;
}
//if( lowest.IntValue > logger.Level.IntValue ) {
// lowest = logger.Level;
//}
if( logger.GetHandlers() != null ) {
foreach( Handler h in new List<Handler>(logger.GetHandlers()) ) {
h.Publish(rec);
}
}
if( logger.UseParentHandlers == false )
break;
logger = logger.Parent;
}
}
}
}