jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
 
using System;
using System.Collections;
using log4net.ObjectRenderer;
using log4net.Core;
using log4net.Util;
using log4net.Plugin;
 
namespace log4net.Repository
{
    /// <summary>
    /// Base implementation of <see cref="ILoggerRepository"/>
    /// </summary>
    /// <remarks>
    /// <para>
    /// Default abstract implementation of the <see cref="ILoggerRepository"/> interface.
    /// </para>
    /// <para>
    /// Skeleton implementation of the <see cref="ILoggerRepository"/> interface.
    /// All <see cref="ILoggerRepository"/> types can extend this type.
    /// </para>
    /// </remarks>
    /// <author>Nicko Cadell</author>
    /// <author>Gert Driesen</author>
    public abstract class LoggerRepositorySkeleton : ILoggerRepository
    {
        #region Member Variables
 
        private string m_name;
        private RendererMap m_rendererMap;
        private PluginMap m_pluginMap;
        private LevelMap m_levelMap;
        private Level m_threshold;
        private bool m_configured;
        private ICollection m_configurationMessages;
        private event LoggerRepositoryShutdownEventHandler m_shutdownEvent;
        private event LoggerRepositoryConfigurationResetEventHandler m_configurationResetEvent;
        private event LoggerRepositoryConfigurationChangedEventHandler m_configurationChangedEvent;
        private PropertiesDictionary m_properties;
 
        #endregion
 
        #region Constructors
 
        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <remarks>
        /// <para>
        /// Initializes the repository with default (empty) properties.
        /// </para>
        /// </remarks>
        protected LoggerRepositorySkeleton() : this(new PropertiesDictionary())
        {
        }
 
        /// <summary>
        /// Construct the repository using specific properties
        /// </summary>
        /// <param name="properties">the properties to set for this repository</param>
        /// <remarks>
        /// <para>
        /// Initializes the repository with specified properties.
        /// </para>
        /// </remarks>
        protected LoggerRepositorySkeleton(PropertiesDictionary properties)
        {
            m_properties = properties;
            m_rendererMap = new RendererMap();
            m_pluginMap = new PluginMap(this);
            m_levelMap = new LevelMap();
            m_configurationMessages = EmptyCollection.Instance;
            m_configured = false;
 
            AddBuiltinLevels();
 
            // Don't disable any levels by default.
            m_threshold = Level.All;
        }
 
        #endregion
 
        #region Implementation of ILoggerRepository
 
        /// <summary>
        /// The name of the repository
        /// </summary>
        /// <value>
        /// The string name of the repository
        /// </value>
        /// <remarks>
        /// <para>
        /// The name of this repository. The name is
        /// used to store and lookup the repositories 
        /// stored by the <see cref="IRepositorySelector"/>.
        /// </para>
        /// </remarks>
        virtual public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }
 
        /// <summary>
        /// The threshold for all events in this repository
        /// </summary>
        /// <value>
        /// The threshold for all events in this repository
        /// </value>
        /// <remarks>
        /// <para>
        /// The threshold for all events in this repository
        /// </para>
        /// </remarks>
        virtual public Level Threshold
        {
            get { return m_threshold; }
            set
            { 
                if (value != null)
                {
                    m_threshold = value; 
                }
                else
                {
                    // Must not set threshold to null
                    LogLog.Warn(declaringType, "LoggerRepositorySkeleton: Threshold cannot be set to null. Setting to ALL");
                    m_threshold = Level.All;
                }
            }
        }
 
        /// <summary>
        /// RendererMap accesses the object renderer map for this repository.
        /// </summary>
        /// <value>
        /// RendererMap accesses the object renderer map for this repository.
        /// </value>
        /// <remarks>
        /// <para>
        /// RendererMap accesses the object renderer map for this repository.
        /// </para>
        /// <para>
        /// The RendererMap holds a mapping between types and
        /// <see cref="IObjectRenderer"/> objects.
        /// </para>
        /// </remarks>
        virtual public RendererMap RendererMap
        {
            get { return m_rendererMap; }
        }
 
        /// <summary>
        /// The plugin map for this repository.
        /// </summary>
        /// <value>
        /// The plugin map for this repository.
        /// </value>
        /// <remarks>
        /// <para>
        /// The plugin map holds the <see cref="IPlugin"/> instances
        /// that have been attached to this repository.
        /// </para>
        /// </remarks>
        virtual public PluginMap PluginMap
        {
            get { return m_pluginMap; }
        }
 
        /// <summary>
        /// Get the level map for the Repository.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Get the level map for the Repository.
        /// </para>
        /// <para>
        /// The level map defines the mappings between
        /// level names and <see cref="Level"/> objects in
        /// this repository.
        /// </para>
        /// </remarks>
        virtual public LevelMap LevelMap
        {
            get { return m_levelMap; }
        }
 
        /// <summary>
        /// Test if logger exists
        /// </summary>
        /// <param name="name">The name of the logger to lookup</param>
        /// <returns>The Logger object with the name specified</returns>
        /// <remarks>
        /// <para>
        /// Check if the named logger exists in the repository. If so return
        /// its reference, otherwise returns <c>null</c>.
        /// </para>
        /// </remarks>
        abstract public ILogger Exists(string name);
 
        /// <summary>
        /// Returns all the currently defined loggers in the repository
        /// </summary>
        /// <returns>All the defined loggers</returns>
        /// <remarks>
        /// <para>
        /// Returns all the currently defined loggers in the repository as an Array.
        /// </para>
        /// </remarks>
        abstract public ILogger[] GetCurrentLoggers();
 
        /// <summary>
        /// Return a new logger instance
        /// </summary>
        /// <param name="name">The name of the logger to retrieve</param>
        /// <returns>The logger object with the name specified</returns>
        /// <remarks>
        /// <para>
        /// Return a new logger instance.
        /// </para>
        /// <para>
        /// If a logger of that name already exists, then it will be
        /// returned. Otherwise, a new logger will be instantiated and
        /// then linked with its existing ancestors as well as children.
        /// </para>
        /// </remarks>
        abstract public ILogger GetLogger(string name);
 
        /// <summary>
        /// Shutdown the repository
        /// </summary>
        /// <remarks>
        /// <para>
        /// Shutdown the repository. Can be overridden in a subclass.
        /// This base class implementation notifies the <see cref="ShutdownEvent"/>
        /// listeners and all attached plugins of the shutdown event.
        /// </para>
        /// </remarks>
        virtual public void Shutdown() 
        {
            // Shutdown attached plugins
            foreach(IPlugin plugin in PluginMap.AllPlugins)
            {
                plugin.Shutdown();
            }
 
            // Notify listeners
            OnShutdown(null);
        }
 
        /// <summary>
        /// Reset the repositories configuration to a default state
        /// </summary>
        /// <remarks>
        /// <para>
        /// Reset all values contained in this instance to their
        /// default state.
        /// </para>
        /// <para>
        /// Existing loggers are not removed. They are just reset.
        /// </para>
        /// <para>
        /// This method should be used sparingly and with care as it will
        /// block all logging until it is completed.
        /// </para>
        /// </remarks>
        virtual public void ResetConfiguration() 
        {
            // Clear internal data structures
            m_rendererMap.Clear();
            m_levelMap.Clear();
            m_configurationMessages = EmptyCollection.Instance;
 
            // Add the predefined levels to the map
            AddBuiltinLevels();
 
            Configured = false;
 
            // Notify listeners
            OnConfigurationReset(null);
        }
 
        /// <summary>
        /// Log the logEvent through this repository.
        /// </summary>
        /// <param name="logEvent">the event to log</param>
        /// <remarks>
        /// <para>
        /// This method should not normally be used to log.
        /// The <see cref="ILog"/> interface should be used 
        /// for routine logging. This interface can be obtained
        /// using the <see cref="log4net.LogManager.GetLogger(string)"/> method.
        /// </para>
        /// <para>
        /// The <c>logEvent</c> is delivered to the appropriate logger and
        /// that logger is then responsible for logging the event.
        /// </para>
        /// </remarks>
        abstract public void Log(LoggingEvent logEvent);
 
        /// <summary>
        /// Flag indicates if this repository has been configured.
        /// </summary>
        /// <value>
        /// Flag indicates if this repository has been configured.
        /// </value>
        /// <remarks>
        /// <para>
        /// Flag indicates if this repository has been configured.
        /// </para>
        /// </remarks>
        virtual public bool Configured 
        { 
            get { return m_configured; }
            set { m_configured = value; }
        }
 
        /// <summary>
        /// Contains a list of internal messages captures during the 
        /// last configuration.
        /// </summary>
        virtual public ICollection ConfigurationMessages
        {
            get { return m_configurationMessages; }
            set { m_configurationMessages = value; }
        }
 
        /// <summary>
        /// Event to notify that the repository has been shutdown.
        /// </summary>
        /// <value>
        /// Event to notify that the repository has been shutdown.
        /// </value>
        /// <remarks>
        /// <para>
        /// Event raised when the repository has been shutdown.
        /// </para>
        /// </remarks>
        public event LoggerRepositoryShutdownEventHandler ShutdownEvent
        {
            add { m_shutdownEvent += value; }
            remove { m_shutdownEvent -= value; }
        }
 
        /// <summary>
        /// Event to notify that the repository has had its configuration reset.
        /// </summary>
        /// <value>
        /// Event to notify that the repository has had its configuration reset.
        /// </value>
        /// <remarks>
        /// <para>
        /// Event raised when the repository's configuration has been
        /// reset to default.
        /// </para>
        /// </remarks>
        public event LoggerRepositoryConfigurationResetEventHandler ConfigurationReset
        {
            add { m_configurationResetEvent += value; }
            remove { m_configurationResetEvent -= value; }
        }
 
        /// <summary>
        /// Event to notify that the repository has had its configuration changed.
        /// </summary>
        /// <value>
        /// Event to notify that the repository has had its configuration changed.
        /// </value>
        /// <remarks>
        /// <para>
        /// Event raised when the repository's configuration has been changed.
        /// </para>
        /// </remarks>
        public event LoggerRepositoryConfigurationChangedEventHandler ConfigurationChanged
        {
            add { m_configurationChangedEvent += value; }
            remove { m_configurationChangedEvent -= value; }
        }
 
        /// <summary>
        /// Repository specific properties
        /// </summary>
        /// <value>
        /// Repository specific properties
        /// </value>
        /// <remarks>
        /// These properties can be specified on a repository specific basis
        /// </remarks>
        public PropertiesDictionary Properties 
        { 
            get { return m_properties; } 
        }
 
        /// <summary>
        /// Returns all the Appenders that are configured as an Array.
        /// </summary>
        /// <returns>All the Appenders</returns>
        /// <remarks>
        /// <para>
        /// Returns all the Appenders that are configured as an Array.
        /// </para>
        /// </remarks>
        abstract public log4net.Appender.IAppender[] GetAppenders();
 
        #endregion
 
        #region Private Static Fields
 
        /// <summary>
        /// The fully qualified type of the LoggerRepositorySkeleton class.
        /// </summary>
        /// <remarks>
        /// Used by the internal logger to record the Type of the
        /// log message.
        /// </remarks>
        private readonly static Type declaringType = typeof(LoggerRepositorySkeleton);
 
        #endregion Private Static Fields
 
        private void AddBuiltinLevels()
        {
            // Add the predefined levels to the map
            m_levelMap.Add(Level.Off);
 
            // Unrecoverable errors
            m_levelMap.Add(Level.Emergency);
            m_levelMap.Add(Level.Fatal);
            m_levelMap.Add(Level.Alert); 
 
            // Recoverable errors
            m_levelMap.Add(Level.Critical); 
            m_levelMap.Add(Level.Severe); 
            m_levelMap.Add(Level.Error); 
            m_levelMap.Add(Level.Warn);
 
            // Information
            m_levelMap.Add(Level.Notice); 
            m_levelMap.Add(Level.Info); 
 
            // Debug
            m_levelMap.Add(Level.Debug);
            m_levelMap.Add(Level.Fine);
            m_levelMap.Add(Level.Trace);
            m_levelMap.Add(Level.Finer);
            m_levelMap.Add(Level.Verbose);
            m_levelMap.Add(Level.Finest);
 
            m_levelMap.Add(Level.All);
        }
 
        /// <summary>
        /// Adds an object renderer for a specific class. 
        /// </summary>
        /// <param name="typeToRender">The type that will be rendered by the renderer supplied.</param>
        /// <param name="rendererInstance">The object renderer used to render the object.</param>
        /// <remarks>
        /// <para>
        /// Adds an object renderer for a specific class. 
        /// </para>
        /// </remarks>
        virtual public void AddRenderer(Type typeToRender, IObjectRenderer rendererInstance) 
        {
            if (typeToRender == null)
            {
                throw new ArgumentNullException("typeToRender");
            }
            if (rendererInstance == null)
            {
                throw new ArgumentNullException("rendererInstance");
            }
 
            m_rendererMap.Put(typeToRender, rendererInstance);
        }
 
        /// <summary>
        /// Notify the registered listeners that the repository is shutting down
        /// </summary>
        /// <param name="e">Empty EventArgs</param>
        /// <remarks>
        /// <para>
        /// Notify any listeners that this repository is shutting down.
        /// </para>
        /// </remarks>
        protected virtual void OnShutdown(EventArgs e)
        {
            if (e == null)
            {
                e = EventArgs.Empty;
            }
 
            LoggerRepositoryShutdownEventHandler handler = m_shutdownEvent;
            if (handler != null)
            {
                handler(this, e);
            }
        }
 
        /// <summary>
        /// Notify the registered listeners that the repository has had its configuration reset
        /// </summary>
        /// <param name="e">Empty EventArgs</param>
        /// <remarks>
        /// <para>
        /// Notify any listeners that this repository's configuration has been reset.
        /// </para>
        /// </remarks>
        protected virtual void OnConfigurationReset(EventArgs e)
        {
            if (e == null)
            {
                e = EventArgs.Empty;
            }
 
            LoggerRepositoryConfigurationResetEventHandler handler = m_configurationResetEvent;
            if (handler != null)
            {
                handler(this, e);
            }
        }
 
        /// <summary>
        /// Notify the registered listeners that the repository has had its configuration changed
        /// </summary>
        /// <param name="e">Empty EventArgs</param>
        /// <remarks>
        /// <para>
        /// Notify any listeners that this repository's configuration has changed.
        /// </para>
        /// </remarks>
        protected virtual void OnConfigurationChanged(EventArgs e)
        {
            if (e == null)
            {
                e = EventArgs.Empty;
            }
 
            LoggerRepositoryConfigurationChangedEventHandler handler = m_configurationChangedEvent;
            if (handler != null)
            {
                handler(this, e);
            }
        }
 
        /// <summary>
        /// Raise a configuration changed event on this repository
        /// </summary>
        /// <param name="e">EventArgs.Empty</param>
        /// <remarks>
        /// <para>
        /// Applications that programmatically change the configuration of the repository should
        /// raise this event notification to notify listeners.
        /// </para>
        /// </remarks>
        public void RaiseConfigurationChanged(EventArgs e)
        {
            OnConfigurationChanged(e);
        }
    }
}