1
#region Copyright & License
3
// Copyright 2001-2005 The Apache Software Foundation
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
20
using System.Collections;
22
namespace log4net.Core
25
/// Defines the default set of levels recognized by the system.
29
/// Each <see cref="LoggingEvent"/> has an associated <see cref="Level"/>.
32
/// Levels have a numeric <see cref="Level.Value"/> that defines the relative
33
/// ordering between levels. Two Levels with the same <see cref="Level.Value"/>
34
/// are deemed to be equivalent.
37
/// The levels that are recognized by log4net are set for each <see cref="log4net.Repository.ILoggerRepository"/>
38
/// and each repository can have different levels defined. The levels are stored
39
/// in the <see cref="log4net.Repository.ILoggerRepository.LevelMap"/> on the repository. Levels are
40
/// looked up by name from the <see cref="log4net.Repository.ILoggerRepository.LevelMap"/>.
43
/// When logging at level INFO the actual level used is not <see cref="Level.Info"/> but
44
/// the value of <c>LoggerRepository.LevelMap["INFO"]</c>. The default value for this is
45
/// <see cref="Level.Info"/>, but this can be changed by reconfiguring the level map.
48
/// Each level has a <see cref="DisplayName"/> in addition to its <see cref="Name"/>. The
49
/// <see cref="DisplayName"/> is the string that is written into the output log. By default
50
/// the display name is the same as the level name, but this can be used to alias levels
51
/// or to localize the log output.
54
/// Some of the predefined levels recognized by the system are:
56
/// <list type="bullet">
58
/// <description><see cref="Off"/>.</description>
61
/// <description><see cref="Fatal"/>.</description>
64
/// <description><see cref="Error"/>.</description>
67
/// <description><see cref="Warn"/>.</description>
70
/// <description><see cref="Info"/>.</description>
73
/// <description><see cref="Debug"/>.</description>
76
/// <description><see cref="All"/>.</description>
80
/// <author>Nicko Cadell</author>
81
/// <author>Gert Driesen</author>
85
sealed public class Level : IComparable
87
#region Public Instance Constructors
92
/// <param name="level">Integer value for this level, higher values represent more severe levels.</param>
93
/// <param name="levelName">The string name of this level.</param>
94
/// <param name="displayName">The display name for this level. This may be localized or otherwise different from the name</param>
97
/// Initializes a new instance of the <see cref="Level" /> class with
98
/// the specified level name and value.
101
public Level(int level, string levelName, string displayName)
103
if (levelName == null)
105
throw new ArgumentNullException("levelName");
107
if (displayName == null)
109
throw new ArgumentNullException("displayName");
112
m_levelValue = level;
113
m_levelName = string.Intern(levelName);
114
m_levelDisplayName = displayName;
120
/// <param name="level">Integer value for this level, higher values represent more severe levels.</param>
121
/// <param name="levelName">The string name of this level.</param>
124
/// Initializes a new instance of the <see cref="Level" /> class with
125
/// the specified level name and value.
128
public Level(int level, string levelName) : this(level, levelName, levelName)
132
#endregion Public Instance Constructors
134
#region Public Instance Properties
137
/// Gets the name of this level.
140
/// The name of this level.
144
/// Gets the name of this level.
149
get { return m_levelName; }
153
/// Gets the value of this level.
156
/// The value of this level.
160
/// Gets the value of this level.
165
get { return m_levelValue; }
169
/// Gets the display name of this level.
172
/// The display name of this level.
176
/// Gets the display name of this level.
179
public string DisplayName
181
get { return m_levelDisplayName; }
184
#endregion Public Instance Properties
186
#region Override implementation of Object
189
/// Returns the <see cref="string" /> representation of the current
190
/// <see cref="Level" />.
193
/// A <see cref="string" /> representation of the current <see cref="Level" />.
197
/// Returns the level <see cref="Name"/>.
200
override public string ToString()
208
/// <param name="o">The object to compare against.</param>
209
/// <returns><c>true</c> if the objects are equal.</returns>
212
/// Compares the levels of <see cref="Level" /> instances, and
213
/// defers to base class if the target object is not a <see cref="Level" />
217
override public bool Equals(object o)
219
Level otherLevel = o as Level;
220
if (otherLevel != null)
222
return m_levelValue == otherLevel.m_levelValue;
226
return base.Equals(o);
231
/// Returns a hash code
233
/// <returns>A hash code for the current <see cref="Level" />.</returns>
236
/// Returns a hash code suitable for use in hashing algorithms and data
237
/// structures like a hash table.
240
/// Returns the hash code of the level <see cref="Value"/>.
243
override public int GetHashCode()
248
#endregion Override implementation of Object
250
#region Implementation of IComparable
253
/// Compares this instance to a specified object and returns an
254
/// indication of their relative values.
256
/// <param name="r">A <see cref="Level"/> instance or <see langword="null" /> to compare with this instance.</param>
258
/// A 32-bit signed integer that indicates the relative order of the
259
/// values compared. The return value has these meanings:
260
/// <list type="table">
262
/// <term>Value</term>
263
/// <description>Meaning</description>
266
/// <term>Less than zero</term>
267
/// <description>This instance is less than <paramref name="r" />.</description>
270
/// <term>Zero</term>
271
/// <description>This instance is equal to <paramref name="r" />.</description>
274
/// <term>Greater than zero</term>
276
/// <para>This instance is greater than <paramref name="r" />.</para>
277
/// <para>-or-</para>
278
/// <para><paramref name="r" /> is <see langword="null" />.</para>
285
/// <paramref name="r" /> must be an instance of <see cref="Level" />
286
/// or <see langword="null" />; otherwise, an exception is thrown.
289
/// <exception cref="ArgumentException"><paramref name="r" /> is not a <see cref="Level" />.</exception>
290
public int CompareTo(object r)
292
Level target = r as Level;
295
return Compare(this, target);
297
throw new ArgumentException("Parameter: r, Value: [" + r + "] is not an instance of Level");
300
#endregion Implementation of IComparable
305
/// Returns a value indicating whether a specified <see cref="Level" />
306
/// is greater than another specified <see cref="Level" />.
308
/// <param name="l">A <see cref="Level" /></param>
309
/// <param name="r">A <see cref="Level" /></param>
311
/// <c>true</c> if <paramref name="l" /> is greater than
312
/// <paramref name="r" />; otherwise, <c>false</c>.
316
/// Compares two levels.
319
public static bool operator > (Level l, Level r)
321
return l.m_levelValue > r.m_levelValue;
325
/// Returns a value indicating whether a specified <see cref="Level" />
326
/// is less than another specified <see cref="Level" />.
328
/// <param name="l">A <see cref="Level" /></param>
329
/// <param name="r">A <see cref="Level" /></param>
331
/// <c>true</c> if <paramref name="l" /> is less than
332
/// <paramref name="r" />; otherwise, <c>false</c>.
336
/// Compares two levels.
339
public static bool operator < (Level l, Level r)
341
return l.m_levelValue < r.m_levelValue;
345
/// Returns a value indicating whether a specified <see cref="Level" />
346
/// is greater than or equal to another specified <see cref="Level" />.
348
/// <param name="l">A <see cref="Level" /></param>
349
/// <param name="r">A <see cref="Level" /></param>
351
/// <c>true</c> if <paramref name="l" /> is greater than or equal to
352
/// <paramref name="r" />; otherwise, <c>false</c>.
356
/// Compares two levels.
359
public static bool operator >= (Level l, Level r)
361
return l.m_levelValue >= r.m_levelValue;
365
/// Returns a value indicating whether a specified <see cref="Level" />
366
/// is less than or equal to another specified <see cref="Level" />.
368
/// <param name="l">A <see cref="Level" /></param>
369
/// <param name="r">A <see cref="Level" /></param>
371
/// <c>true</c> if <paramref name="l" /> is less than or equal to
372
/// <paramref name="r" />; otherwise, <c>false</c>.
376
/// Compares two levels.
379
public static bool operator <= (Level l, Level r)
381
return l.m_levelValue <= r.m_levelValue;
385
/// Returns a value indicating whether two specified <see cref="Level" />
386
/// objects have the same value.
388
/// <param name="l">A <see cref="Level" /> or <see langword="null" />.</param>
389
/// <param name="r">A <see cref="Level" /> or <see langword="null" />.</param>
391
/// <c>true</c> if the value of <paramref name="l" /> is the same as the
392
/// value of <paramref name="r" />; otherwise, <c>false</c>.
396
/// Compares two levels.
399
public static bool operator == (Level l, Level r)
401
if (((object)l) != null && ((object)r) != null)
403
return l.m_levelValue == r.m_levelValue;
407
return ((object) l) == ((object) r);
412
/// Returns a value indicating whether two specified <see cref="Level" />
413
/// objects have different values.
415
/// <param name="l">A <see cref="Level" /> or <see langword="null" />.</param>
416
/// <param name="r">A <see cref="Level" /> or <see langword="null" />.</param>
418
/// <c>true</c> if the value of <paramref name="l" /> is different from
419
/// the value of <paramref name="r" />; otherwise, <c>false</c>.
423
/// Compares two levels.
426
public static bool operator != (Level l, Level r)
433
#region Public Static Methods
436
/// Compares two specified <see cref="Level"/> instances.
438
/// <param name="l">The first <see cref="Level"/> to compare.</param>
439
/// <param name="r">The second <see cref="Level"/> to compare.</param>
441
/// A 32-bit signed integer that indicates the relative order of the
442
/// two values compared. The return value has these meanings:
443
/// <list type="table">
445
/// <term>Value</term>
446
/// <description>Meaning</description>
449
/// <term>Less than zero</term>
450
/// <description><paramref name="l" /> is less than <paramref name="r" />.</description>
453
/// <term>Zero</term>
454
/// <description><paramref name="l" /> is equal to <paramref name="r" />.</description>
457
/// <term>Greater than zero</term>
458
/// <description><paramref name="l" /> is greater than <paramref name="r" />.</description>
464
/// Compares two levels.
467
public static int Compare(Level l, Level r)
470
if ((object)l == (object)r)
475
if (l == null && r == null)
488
return l.m_levelValue - r.m_levelValue;
491
#endregion Public Static Methods
493
#region Public Static Fields
496
/// The <see cref="Off" /> level designates a higher level than all the rest.
498
public readonly static Level Off = new Level(int.MaxValue, "OFF");
501
/// The <see cref="Emergency" /> level designates very severe error events.
502
/// System unusable, emergencies.
504
public readonly static Level Emergency = new Level(120000, "EMERGENCY");
507
/// The <see cref="Fatal" /> level designates very severe error events
508
/// that will presumably lead the application to abort.
510
public readonly static Level Fatal = new Level(110000, "FATAL");
513
/// The <see cref="Alert" /> level designates very severe error events.
514
/// Take immediate action, alerts.
516
public readonly static Level Alert = new Level(100000, "ALERT");
519
/// The <see cref="Critical" /> level designates very severe error events.
520
/// Critical condition, critical.
522
public readonly static Level Critical = new Level(90000, "CRITICAL");
525
/// The <see cref="Severe" /> level designates very severe error events.
527
public readonly static Level Severe = new Level(80000, "SEVERE");
530
/// The <see cref="Error" /> level designates error events that might
531
/// still allow the application to continue running.
533
public readonly static Level Error = new Level(70000, "ERROR");
536
/// The <see cref="Warn" /> level designates potentially harmful
539
public readonly static Level Warn = new Level(60000, "WARN");
542
/// The <see cref="Notice" /> level designates informational messages
543
/// that highlight the progress of the application at the highest level.
545
public readonly static Level Notice = new Level(50000, "NOTICE");
548
/// The <see cref="Info" /> level designates informational messages that
549
/// highlight the progress of the application at coarse-grained level.
551
public readonly static Level Info = new Level(40000, "INFO");
554
/// The <see cref="Debug" /> level designates fine-grained informational
555
/// events that are most useful to debug an application.
557
public readonly static Level Debug = new Level(30000, "DEBUG");
560
/// The <see cref="Fine" /> level designates fine-grained informational
561
/// events that are most useful to debug an application.
563
public readonly static Level Fine = new Level(30000, "FINE");
566
/// The <see cref="Trace" /> level designates fine-grained informational
567
/// events that are most useful to debug an application.
569
public readonly static Level Trace = new Level(20000, "TRACE");
572
/// The <see cref="Finer" /> level designates fine-grained informational
573
/// events that are most useful to debug an application.
575
public readonly static Level Finer = new Level(20000, "FINER");
578
/// The <see cref="Verbose" /> level designates fine-grained informational
579
/// events that are most useful to debug an application.
581
public readonly static Level Verbose = new Level(10000, "VERBOSE");
584
/// The <see cref="Finest" /> level designates fine-grained informational
585
/// events that are most useful to debug an application.
587
public readonly static Level Finest = new Level(10000, "FINEST");
590
/// The <see cref="All" /> level designates the lowest level possible.
592
public readonly static Level All = new Level(int.MinValue, "ALL");
594
#endregion Public Static Fields
596
#region Private Instance Fields
598
private readonly int m_levelValue;
599
private readonly string m_levelName;
600
private readonly string m_levelDisplayName;
602
#endregion Private Instance Fields