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.
21
using log4net.ObjectRenderer;
26
namespace log4net.Repository
29
/// Base implementation of <see cref="ILoggerRepository"/>
33
/// Default abstract implementation of the <see cref="ILoggerRepository"/> interface.
36
/// Skeleton implementation of the <see cref="ILoggerRepository"/> interface.
37
/// All <see cref="ILoggerRepository"/> types can extend this type.
40
/// <author>Nicko Cadell</author>
41
/// <author>Gert Driesen</author>
42
public abstract class LoggerRepositorySkeleton : ILoggerRepository
44
#region Member Variables
46
private string m_name;
47
private RendererMap m_rendererMap;
48
private PluginMap m_pluginMap;
49
private LevelMap m_levelMap;
50
private Level m_threshold;
51
private bool m_configured;
52
private event LoggerRepositoryShutdownEventHandler m_shutdownEvent;
53
private event LoggerRepositoryConfigurationResetEventHandler m_configurationResetEvent;
54
private event LoggerRepositoryConfigurationChangedEventHandler m_configurationChangedEvent;
55
private PropertiesDictionary m_properties;
62
/// Default Constructor
66
/// Initializes the repository with default (empty) properties.
69
protected LoggerRepositorySkeleton() : this(new PropertiesDictionary())
74
/// Construct the repository using specific properties
76
/// <param name="properties">the properties to set for this repository</param>
79
/// Initializes the repository with specified properties.
82
protected LoggerRepositorySkeleton(PropertiesDictionary properties)
84
m_properties = properties;
85
m_rendererMap = new RendererMap();
86
m_pluginMap = new PluginMap(this);
87
m_levelMap = new LevelMap();
92
// Don't disable any levels by default.
93
m_threshold = Level.All;
98
#region Implementation of ILoggerRepository
101
/// The name of the repository
104
/// The string name of the repository
108
/// The name of this repository. The name is
109
/// used to store and lookup the repositories
110
/// stored by the <see cref="IRepositorySelector"/>.
113
virtual public string Name
115
get { return m_name; }
116
set { m_name = value; }
120
/// The threshold for all events in this repository
123
/// The threshold for all events in this repository
127
/// The threshold for all events in this repository
130
virtual public Level Threshold
132
get { return m_threshold; }
141
// Must not set threshold to null
142
LogLog.Warn("LoggerRepositorySkeleton: Threshold cannot be set to null. Setting to ALL");
143
m_threshold = Level.All;
149
/// RendererMap accesses the object renderer map for this repository.
152
/// RendererMap accesses the object renderer map for this repository.
156
/// RendererMap accesses the object renderer map for this repository.
159
/// The RendererMap holds a mapping between types and
160
/// <see cref="IObjectRenderer"/> objects.
163
virtual public RendererMap RendererMap
165
get { return m_rendererMap; }
169
/// The plugin map for this repository.
172
/// The plugin map for this repository.
176
/// The plugin map holds the <see cref="IPlugin"/> instances
177
/// that have been attached to this repository.
180
virtual public PluginMap PluginMap
182
get { return m_pluginMap; }
186
/// Get the level map for the Repository.
190
/// Get the level map for the Repository.
193
/// The level map defines the mappings between
194
/// level names and <see cref="Level"/> objects in
198
virtual public LevelMap LevelMap
200
get { return m_levelMap; }
204
/// Test if logger exists
206
/// <param name="name">The name of the logger to lookup</param>
207
/// <returns>The Logger object with the name specified</returns>
210
/// Check if the named logger exists in the repository. If so return
211
/// its reference, otherwise returns <c>null</c>.
214
abstract public ILogger Exists(string name);
217
/// Returns all the currently defined loggers in the repository
219
/// <returns>All the defined loggers</returns>
222
/// Returns all the currently defined loggers in the repository as an Array.
225
abstract public ILogger[] GetCurrentLoggers();
228
/// Return a new logger instance
230
/// <param name="name">The name of the logger to retrieve</param>
231
/// <returns>The logger object with the name specified</returns>
234
/// Return a new logger instance.
237
/// If a logger of that name already exists, then it will be
238
/// returned. Otherwise, a new logger will be instantiated and
239
/// then linked with its existing ancestors as well as children.
242
abstract public ILogger GetLogger(string name);
245
/// Shutdown the repository
249
/// Shutdown the repository. Can be overridden in a subclass.
250
/// This base class implementation notifies the <see cref="ShutdownEvent"/>
251
/// listeners and all attached plugins of the shutdown event.
254
virtual public void Shutdown()
256
// Shutdown attached plugins
257
foreach(IPlugin plugin in PluginMap.AllPlugins)
267
/// Reset the repositories configuration to a default state
271
/// Reset all values contained in this instance to their
275
/// Existing loggers are not removed. They are just reset.
278
/// This method should be used sparingly and with care as it will
279
/// block all logging until it is completed.
282
virtual public void ResetConfiguration()
284
// Clear internal data structures
285
m_rendererMap.Clear();
288
// Add the predefined levels to the map
294
OnConfigurationReset(null);
298
/// Log the logEvent through this repository.
300
/// <param name="logEvent">the event to log</param>
303
/// This method should not normally be used to log.
304
/// The <see cref="ILog"/> interface should be used
305
/// for routine logging. This interface can be obtained
306
/// using the <see cref="log4net.LogManager.GetLogger(string)"/> method.
309
/// The <c>logEvent</c> is delivered to the appropriate logger and
310
/// that logger is then responsible for logging the event.
313
abstract public void Log(LoggingEvent logEvent);
316
/// Flag indicates if this repository has been configured.
319
/// Flag indicates if this repository has been configured.
323
/// Flag indicates if this repository has been configured.
326
virtual public bool Configured
328
get { return m_configured; }
329
set { m_configured = value; }
333
/// Event to notify that the repository has been shutdown.
336
/// Event to notify that the repository has been shutdown.
340
/// Event raised when the repository has been shutdown.
343
public event LoggerRepositoryShutdownEventHandler ShutdownEvent
345
add { m_shutdownEvent += value; }
346
remove { m_shutdownEvent -= value; }
350
/// Event to notify that the repository has had its configuration reset.
353
/// Event to notify that the repository has had its configuration reset.
357
/// Event raised when the repository's configuration has been
358
/// reset to default.
361
public event LoggerRepositoryConfigurationResetEventHandler ConfigurationReset
363
add { m_configurationResetEvent += value; }
364
remove { m_configurationResetEvent -= value; }
368
/// Event to notify that the repository has had its configuration changed.
371
/// Event to notify that the repository has had its configuration changed.
375
/// Event raised when the repository's configuration has been changed.
378
public event LoggerRepositoryConfigurationChangedEventHandler ConfigurationChanged
380
add { m_configurationChangedEvent += value; }
381
remove { m_configurationChangedEvent -= value; }
385
/// Repository specific properties
388
/// Repository specific properties
391
/// These properties can be specified on a repository specific basis
393
public PropertiesDictionary Properties
395
get { return m_properties; }
399
/// Returns all the Appenders that are configured as an Array.
401
/// <returns>All the Appenders</returns>
404
/// Returns all the Appenders that are configured as an Array.
407
abstract public log4net.Appender.IAppender[] GetAppenders();
411
private void AddBuiltinLevels()
413
// Add the predefined levels to the map
414
m_levelMap.Add(Level.Off);
416
// Unrecoverable errors
417
m_levelMap.Add(Level.Emergency);
418
m_levelMap.Add(Level.Fatal);
419
m_levelMap.Add(Level.Alert);
421
// Recoverable errors
422
m_levelMap.Add(Level.Critical);
423
m_levelMap.Add(Level.Severe);
424
m_levelMap.Add(Level.Error);
425
m_levelMap.Add(Level.Warn);
428
m_levelMap.Add(Level.Notice);
429
m_levelMap.Add(Level.Info);
432
m_levelMap.Add(Level.Debug);
433
m_levelMap.Add(Level.Fine);
434
m_levelMap.Add(Level.Trace);
435
m_levelMap.Add(Level.Finer);
436
m_levelMap.Add(Level.Verbose);
437
m_levelMap.Add(Level.Finest);
439
m_levelMap.Add(Level.All);
443
/// Adds an object renderer for a specific class.
445
/// <param name="typeToRender">The type that will be rendered by the renderer supplied.</param>
446
/// <param name="rendererInstance">The object renderer used to render the object.</param>
449
/// Adds an object renderer for a specific class.
452
virtual public void AddRenderer(Type typeToRender, IObjectRenderer rendererInstance)
454
if (typeToRender == null)
456
throw new ArgumentNullException("typeToRender");
458
if (rendererInstance == null)
460
throw new ArgumentNullException("rendererInstance");
463
m_rendererMap.Put(typeToRender, rendererInstance);
467
/// Notify the registered listeners that the repository is shutting down
469
/// <param name="e">Empty EventArgs</param>
472
/// Notify any listeners that this repository is shutting down.
475
protected virtual void OnShutdown(EventArgs e)
482
LoggerRepositoryShutdownEventHandler handler = m_shutdownEvent;
490
/// Notify the registered listeners that the repository has had its configuration reset
492
/// <param name="e">Empty EventArgs</param>
495
/// Notify any listeners that this repository's configuration has been reset.
498
protected virtual void OnConfigurationReset(EventArgs e)
505
LoggerRepositoryConfigurationResetEventHandler handler = m_configurationResetEvent;
513
/// Notify the registered listeners that the repository has had its configuration changed
515
/// <param name="e">Empty EventArgs</param>
518
/// Notify any listeners that this repository's configuration has changed.
521
protected virtual void OnConfigurationChanged(EventArgs e)
528
LoggerRepositoryConfigurationChangedEventHandler handler = m_configurationChangedEvent;
531
handler(this, EventArgs.Empty);
536
/// Raise a configuration changed event on this repository
538
/// <param name="e">EventArgs.Empty</param>
541
/// Applications that programmatically change the configuration of the repository should
542
/// raise this event notification to notify listeners.
545
public void RaiseConfigurationChanged(EventArgs e)
547
OnConfigurationChanged(e);