~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Simias.log4net/src/Repository/.svn/text-base/LoggerRepositorySkeleton.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#region Copyright & License
2
 
//
3
 
// Copyright 2001-2005 The Apache Software Foundation
4
 
//
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
8
 
//
9
 
// http://www.apache.org/licenses/LICENSE-2.0
10
 
//
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.
16
 
//
17
 
#endregion
18
 
 
19
 
using System;
20
 
 
21
 
using log4net.ObjectRenderer;
22
 
using log4net.Core;
23
 
using log4net.Util;
24
 
using log4net.Plugin;
25
 
 
26
 
namespace log4net.Repository
27
 
{
28
 
        /// <summary>
29
 
        /// Base implementation of <see cref="ILoggerRepository"/>
30
 
        /// </summary>
31
 
        /// <remarks>
32
 
        /// <para>
33
 
        /// Default abstract implementation of the <see cref="ILoggerRepository"/> interface.
34
 
        /// </para>
35
 
        /// <para>
36
 
        /// Skeleton implementation of the <see cref="ILoggerRepository"/> interface.
37
 
        /// All <see cref="ILoggerRepository"/> types can extend this type.
38
 
        /// </para>
39
 
        /// </remarks>
40
 
        /// <author>Nicko Cadell</author>
41
 
        /// <author>Gert Driesen</author>
42
 
        public abstract class LoggerRepositorySkeleton : ILoggerRepository
43
 
        {
44
 
                #region Member Variables
45
 
 
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;
56
 
 
57
 
                #endregion
58
 
 
59
 
                #region Constructors
60
 
 
61
 
                /// <summary>
62
 
                /// Default Constructor
63
 
                /// </summary>
64
 
                /// <remarks>
65
 
                /// <para>
66
 
                /// Initializes the repository with default (empty) properties.
67
 
                /// </para>
68
 
                /// </remarks>
69
 
                protected LoggerRepositorySkeleton() : this(new PropertiesDictionary())
70
 
                {
71
 
                }
72
 
 
73
 
                /// <summary>
74
 
                /// Construct the repository using specific properties
75
 
                /// </summary>
76
 
                /// <param name="properties">the properties to set for this repository</param>
77
 
                /// <remarks>
78
 
                /// <para>
79
 
                /// Initializes the repository with specified properties.
80
 
                /// </para>
81
 
                /// </remarks>
82
 
                protected LoggerRepositorySkeleton(PropertiesDictionary properties)
83
 
                {
84
 
                        m_properties = properties;
85
 
                        m_rendererMap = new RendererMap();
86
 
                        m_pluginMap = new PluginMap(this);
87
 
                        m_levelMap = new LevelMap();
88
 
                        m_configured = false;
89
 
 
90
 
                        AddBuiltinLevels();
91
 
 
92
 
                        // Don't disable any levels by default.
93
 
                        m_threshold = Level.All;
94
 
                }
95
 
 
96
 
                #endregion
97
 
 
98
 
                #region Implementation of ILoggerRepository
99
 
 
100
 
                /// <summary>
101
 
                /// The name of the repository
102
 
                /// </summary>
103
 
                /// <value>
104
 
                /// The string name of the repository
105
 
                /// </value>
106
 
                /// <remarks>
107
 
                /// <para>
108
 
                /// The name of this repository. The name is
109
 
                /// used to store and lookup the repositories 
110
 
                /// stored by the <see cref="IRepositorySelector"/>.
111
 
                /// </para>
112
 
                /// </remarks>
113
 
                virtual public string Name
114
 
                {
115
 
                        get { return m_name; }
116
 
                        set { m_name = value; }
117
 
                }
118
 
 
119
 
                /// <summary>
120
 
                /// The threshold for all events in this repository
121
 
                /// </summary>
122
 
                /// <value>
123
 
                /// The threshold for all events in this repository
124
 
                /// </value>
125
 
                /// <remarks>
126
 
                /// <para>
127
 
                /// The threshold for all events in this repository
128
 
                /// </para>
129
 
                /// </remarks>
130
 
                virtual public Level Threshold
131
 
                {
132
 
                        get { return m_threshold; }
133
 
                        set
134
 
                        { 
135
 
                                if (value != null)
136
 
                                {
137
 
                                        m_threshold = value; 
138
 
                                }
139
 
                                else
140
 
                                {
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;
144
 
                                }
145
 
                        }
146
 
                }
147
 
 
148
 
                /// <summary>
149
 
                /// RendererMap accesses the object renderer map for this repository.
150
 
                /// </summary>
151
 
                /// <value>
152
 
                /// RendererMap accesses the object renderer map for this repository.
153
 
                /// </value>
154
 
                /// <remarks>
155
 
                /// <para>
156
 
                /// RendererMap accesses the object renderer map for this repository.
157
 
                /// </para>
158
 
                /// <para>
159
 
                /// The RendererMap holds a mapping between types and
160
 
                /// <see cref="IObjectRenderer"/> objects.
161
 
                /// </para>
162
 
                /// </remarks>
163
 
                virtual public RendererMap RendererMap
164
 
                {
165
 
                        get { return m_rendererMap; }
166
 
                }
167
 
 
168
 
                /// <summary>
169
 
                /// The plugin map for this repository.
170
 
                /// </summary>
171
 
                /// <value>
172
 
                /// The plugin map for this repository.
173
 
                /// </value>
174
 
                /// <remarks>
175
 
                /// <para>
176
 
                /// The plugin map holds the <see cref="IPlugin"/> instances
177
 
                /// that have been attached to this repository.
178
 
                /// </para>
179
 
                /// </remarks>
180
 
                virtual public PluginMap PluginMap
181
 
                {
182
 
                        get { return m_pluginMap; }
183
 
                }
184
 
 
185
 
                /// <summary>
186
 
                /// Get the level map for the Repository.
187
 
                /// </summary>
188
 
                /// <remarks>
189
 
                /// <para>
190
 
                /// Get the level map for the Repository.
191
 
                /// </para>
192
 
                /// <para>
193
 
                /// The level map defines the mappings between
194
 
                /// level names and <see cref="Level"/> objects in
195
 
                /// this repository.
196
 
                /// </para>
197
 
                /// </remarks>
198
 
                virtual public LevelMap LevelMap
199
 
                {
200
 
                        get { return m_levelMap; }
201
 
                }
202
 
 
203
 
                /// <summary>
204
 
                /// Test if logger exists
205
 
                /// </summary>
206
 
                /// <param name="name">The name of the logger to lookup</param>
207
 
                /// <returns>The Logger object with the name specified</returns>
208
 
                /// <remarks>
209
 
                /// <para>
210
 
                /// Check if the named logger exists in the repository. If so return
211
 
                /// its reference, otherwise returns <c>null</c>.
212
 
                /// </para>
213
 
                /// </remarks>
214
 
                abstract public ILogger Exists(string name);
215
 
 
216
 
                /// <summary>
217
 
                /// Returns all the currently defined loggers in the repository
218
 
                /// </summary>
219
 
                /// <returns>All the defined loggers</returns>
220
 
                /// <remarks>
221
 
                /// <para>
222
 
                /// Returns all the currently defined loggers in the repository as an Array.
223
 
                /// </para>
224
 
                /// </remarks>
225
 
                abstract public ILogger[] GetCurrentLoggers();
226
 
 
227
 
                /// <summary>
228
 
                /// Return a new logger instance
229
 
                /// </summary>
230
 
                /// <param name="name">The name of the logger to retrieve</param>
231
 
                /// <returns>The logger object with the name specified</returns>
232
 
                /// <remarks>
233
 
                /// <para>
234
 
                /// Return a new logger instance.
235
 
                /// </para>
236
 
                /// <para>
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.
240
 
                /// </para>
241
 
                /// </remarks>
242
 
                abstract public ILogger GetLogger(string name);
243
 
 
244
 
                /// <summary>
245
 
                /// Shutdown the repository
246
 
                /// </summary>
247
 
                /// <remarks>
248
 
                /// <para>
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.
252
 
                /// </para>
253
 
                /// </remarks>
254
 
                virtual public void Shutdown() 
255
 
                {
256
 
                        // Shutdown attached plugins
257
 
                        foreach(IPlugin plugin in PluginMap.AllPlugins)
258
 
                        {
259
 
                                plugin.Shutdown();
260
 
                        }
261
 
 
262
 
                        // Notify listeners
263
 
                        OnShutdown(null);
264
 
                }
265
 
 
266
 
                /// <summary>
267
 
                /// Reset the repositories configuration to a default state
268
 
                /// </summary>
269
 
                /// <remarks>
270
 
                /// <para>
271
 
                /// Reset all values contained in this instance to their
272
 
                /// default state.
273
 
                /// </para>
274
 
                /// <para>
275
 
                /// Existing loggers are not removed. They are just reset.
276
 
                /// </para>
277
 
                /// <para>
278
 
                /// This method should be used sparingly and with care as it will
279
 
                /// block all logging until it is completed.
280
 
                /// </para>
281
 
                /// </remarks>
282
 
                virtual public void ResetConfiguration() 
283
 
                {
284
 
                        // Clear internal data structures
285
 
                        m_rendererMap.Clear();
286
 
                        m_levelMap.Clear();
287
 
 
288
 
                        // Add the predefined levels to the map
289
 
                        AddBuiltinLevels();
290
 
 
291
 
                        Configured = false;
292
 
 
293
 
                        // Notify listeners
294
 
                        OnConfigurationReset(null);
295
 
                }
296
 
 
297
 
                /// <summary>
298
 
                /// Log the logEvent through this repository.
299
 
                /// </summary>
300
 
                /// <param name="logEvent">the event to log</param>
301
 
                /// <remarks>
302
 
                /// <para>
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.
307
 
                /// </para>
308
 
                /// <para>
309
 
                /// The <c>logEvent</c> is delivered to the appropriate logger and
310
 
                /// that logger is then responsible for logging the event.
311
 
                /// </para>
312
 
                /// </remarks>
313
 
                abstract public void Log(LoggingEvent logEvent);
314
 
 
315
 
                /// <summary>
316
 
                /// Flag indicates if this repository has been configured.
317
 
                /// </summary>
318
 
                /// <value>
319
 
                /// Flag indicates if this repository has been configured.
320
 
                /// </value>
321
 
                /// <remarks>
322
 
                /// <para>
323
 
                /// Flag indicates if this repository has been configured.
324
 
                /// </para>
325
 
                /// </remarks>
326
 
                virtual public bool Configured 
327
 
                { 
328
 
                        get { return m_configured; }
329
 
                        set { m_configured = value; }
330
 
                }
331
 
 
332
 
                /// <summary>
333
 
                /// Event to notify that the repository has been shutdown.
334
 
                /// </summary>
335
 
                /// <value>
336
 
                /// Event to notify that the repository has been shutdown.
337
 
                /// </value>
338
 
                /// <remarks>
339
 
                /// <para>
340
 
                /// Event raised when the repository has been shutdown.
341
 
                /// </para>
342
 
                /// </remarks>
343
 
                public event LoggerRepositoryShutdownEventHandler ShutdownEvent
344
 
                {
345
 
                        add { m_shutdownEvent += value; }
346
 
                        remove { m_shutdownEvent -= value; }
347
 
                }
348
 
 
349
 
                /// <summary>
350
 
                /// Event to notify that the repository has had its configuration reset.
351
 
                /// </summary>
352
 
                /// <value>
353
 
                /// Event to notify that the repository has had its configuration reset.
354
 
                /// </value>
355
 
                /// <remarks>
356
 
                /// <para>
357
 
                /// Event raised when the repository's configuration has been
358
 
                /// reset to default.
359
 
                /// </para>
360
 
                /// </remarks>
361
 
                public event LoggerRepositoryConfigurationResetEventHandler ConfigurationReset
362
 
                {
363
 
                        add { m_configurationResetEvent += value; }
364
 
                        remove { m_configurationResetEvent -= value; }
365
 
                }
366
 
 
367
 
                /// <summary>
368
 
                /// Event to notify that the repository has had its configuration changed.
369
 
                /// </summary>
370
 
                /// <value>
371
 
                /// Event to notify that the repository has had its configuration changed.
372
 
                /// </value>
373
 
                /// <remarks>
374
 
                /// <para>
375
 
                /// Event raised when the repository's configuration has been changed.
376
 
                /// </para>
377
 
                /// </remarks>
378
 
                public event LoggerRepositoryConfigurationChangedEventHandler ConfigurationChanged
379
 
                {
380
 
                        add { m_configurationChangedEvent += value; }
381
 
                        remove { m_configurationChangedEvent -= value; }
382
 
                }
383
 
 
384
 
                /// <summary>
385
 
                /// Repository specific properties
386
 
                /// </summary>
387
 
                /// <value>
388
 
                /// Repository specific properties
389
 
                /// </value>
390
 
                /// <remarks>
391
 
                /// These properties can be specified on a repository specific basis
392
 
                /// </remarks>
393
 
                public PropertiesDictionary Properties 
394
 
                { 
395
 
                        get { return m_properties; } 
396
 
                }
397
 
 
398
 
                /// <summary>
399
 
                /// Returns all the Appenders that are configured as an Array.
400
 
                /// </summary>
401
 
                /// <returns>All the Appenders</returns>
402
 
                /// <remarks>
403
 
                /// <para>
404
 
                /// Returns all the Appenders that are configured as an Array.
405
 
                /// </para>
406
 
                /// </remarks>
407
 
                abstract public log4net.Appender.IAppender[] GetAppenders();
408
 
 
409
 
                #endregion
410
 
 
411
 
                private void AddBuiltinLevels()
412
 
                {
413
 
                        // Add the predefined levels to the map
414
 
                        m_levelMap.Add(Level.Off);
415
 
 
416
 
                        // Unrecoverable errors
417
 
                        m_levelMap.Add(Level.Emergency);
418
 
                        m_levelMap.Add(Level.Fatal);
419
 
                        m_levelMap.Add(Level.Alert); 
420
 
 
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);
426
 
 
427
 
                        // Information
428
 
                        m_levelMap.Add(Level.Notice); 
429
 
                        m_levelMap.Add(Level.Info); 
430
 
 
431
 
                        // Debug
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);
438
 
 
439
 
                        m_levelMap.Add(Level.All);
440
 
                }
441
 
 
442
 
                /// <summary>
443
 
                /// Adds an object renderer for a specific class. 
444
 
                /// </summary>
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>
447
 
                /// <remarks>
448
 
                /// <para>
449
 
                /// Adds an object renderer for a specific class. 
450
 
                /// </para>
451
 
                /// </remarks>
452
 
                virtual public void AddRenderer(Type typeToRender, IObjectRenderer rendererInstance) 
453
 
                {
454
 
                        if (typeToRender == null)
455
 
                        {
456
 
                                throw new ArgumentNullException("typeToRender");
457
 
                        }
458
 
                        if (rendererInstance == null)
459
 
                        {
460
 
                                throw new ArgumentNullException("rendererInstance");
461
 
                        }
462
 
 
463
 
                        m_rendererMap.Put(typeToRender, rendererInstance);
464
 
                }
465
 
 
466
 
                /// <summary>
467
 
                /// Notify the registered listeners that the repository is shutting down
468
 
                /// </summary>
469
 
                /// <param name="e">Empty EventArgs</param>
470
 
                /// <remarks>
471
 
                /// <para>
472
 
                /// Notify any listeners that this repository is shutting down.
473
 
                /// </para>
474
 
                /// </remarks>
475
 
                protected virtual void OnShutdown(EventArgs e)
476
 
                {
477
 
                        if (e == null)
478
 
                        {
479
 
                                e = EventArgs.Empty;
480
 
                        }
481
 
 
482
 
                        LoggerRepositoryShutdownEventHandler handler = m_shutdownEvent;
483
 
                        if (handler != null)
484
 
                        {
485
 
                                handler(this, e);
486
 
                        }
487
 
                }
488
 
 
489
 
                /// <summary>
490
 
                /// Notify the registered listeners that the repository has had its configuration reset
491
 
                /// </summary>
492
 
                /// <param name="e">Empty EventArgs</param>
493
 
                /// <remarks>
494
 
                /// <para>
495
 
                /// Notify any listeners that this repository's configuration has been reset.
496
 
                /// </para>
497
 
                /// </remarks>
498
 
                protected virtual void OnConfigurationReset(EventArgs e)
499
 
                {
500
 
                        if (e == null)
501
 
                        {
502
 
                                e = EventArgs.Empty;
503
 
                        }
504
 
 
505
 
                        LoggerRepositoryConfigurationResetEventHandler handler = m_configurationResetEvent;
506
 
                        if (handler != null)
507
 
                        {
508
 
                                handler(this, e);
509
 
                        }
510
 
                }
511
 
 
512
 
                /// <summary>
513
 
                /// Notify the registered listeners that the repository has had its configuration changed
514
 
                /// </summary>
515
 
                /// <param name="e">Empty EventArgs</param>
516
 
                /// <remarks>
517
 
                /// <para>
518
 
                /// Notify any listeners that this repository's configuration has changed.
519
 
                /// </para>
520
 
                /// </remarks>
521
 
                protected virtual void OnConfigurationChanged(EventArgs e)
522
 
                {
523
 
                        if (e == null)
524
 
                        {
525
 
                                e = EventArgs.Empty;
526
 
                        }
527
 
 
528
 
                        LoggerRepositoryConfigurationChangedEventHandler handler = m_configurationChangedEvent;
529
 
                        if (handler != null)
530
 
                        {
531
 
                                handler(this, EventArgs.Empty);
532
 
                        }
533
 
                }
534
 
 
535
 
                /// <summary>
536
 
                /// Raise a configuration changed event on this repository
537
 
                /// </summary>
538
 
                /// <param name="e">EventArgs.Empty</param>
539
 
                /// <remarks>
540
 
                /// <para>
541
 
                /// Applications that programmatically change the configuration of the repository should
542
 
                /// raise this event notification to notify listeners.
543
 
                /// </para>
544
 
                /// </remarks>
545
 
                public void RaiseConfigurationChanged(EventArgs e)
546
 
                {
547
 
                        OnConfigurationChanged(e);
548
 
                }
549
 
        }
550
 
}