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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#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
 
// .NET Compact Framework 1.0 has no support for reading assembly attributes
// and uses the CompactRepositorySelector instead
#if !NETCF
 
using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Reflection;
 
using log4net.Config;
using log4net.Util;
using log4net.Repository;
 
namespace log4net.Core
{
    /// <summary>
    /// The default implementation of the <see cref="IRepositorySelector"/> interface.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Uses attributes defined on the calling assembly to determine how to
    /// configure the hierarchy for the repository.
    /// </para>
    /// </remarks>
    /// <author>Nicko Cadell</author>
    /// <author>Gert Driesen</author>
    public class DefaultRepositorySelector : IRepositorySelector
    {
        #region Public Events
 
        /// <summary>
        /// Event to notify that a logger repository has been created.
        /// </summary>
        /// <value>
        /// Event to notify that a logger repository has been created.
        /// </value>
        /// <remarks>
        /// <para>
        /// Event raised when a new repository is created.
        /// The event source will be this selector. The event args will
        /// be a <see cref="LoggerRepositoryCreationEventArgs"/> which
        /// holds the newly created <see cref="ILoggerRepository"/>.
        /// </para>
        /// </remarks>
        public event LoggerRepositoryCreationEventHandler LoggerRepositoryCreatedEvent 
        {
            add { m_loggerRepositoryCreatedEvent += value; }
            remove { m_loggerRepositoryCreatedEvent -= value; }
        }
 
        #endregion Public Events
 
        #region Public Instance Constructors
 
        /// <summary>
        /// Creates a new repository selector.
        /// </summary>
        /// <param name="defaultRepositoryType">The type of the repositories to create, must implement <see cref="ILoggerRepository"/></param>
        /// <remarks>
        /// <para>
        /// Create an new repository selector.
        /// The default type for repositories must be specified,
        /// an appropriate value would be <see cref="log4net.Repository.Hierarchy.Hierarchy"/>.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="defaultRepositoryType"/> is <see langword="null" />.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="defaultRepositoryType"/> does not implement <see cref="ILoggerRepository"/>.</exception>
        public DefaultRepositorySelector(Type defaultRepositoryType)
        {
            if (defaultRepositoryType == null)
            {
                throw new ArgumentNullException("defaultRepositoryType");
            }
 
            // Check that the type is a repository
            if (! (typeof(ILoggerRepository).IsAssignableFrom(defaultRepositoryType)) )
            {
                throw log4net.Util.SystemInfo.CreateArgumentOutOfRangeException("defaultRepositoryType", defaultRepositoryType, "Parameter: defaultRepositoryType, Value: [" + defaultRepositoryType + "] out of range. Argument must implement the ILoggerRepository interface");
            }
 
            m_defaultRepositoryType = defaultRepositoryType;
 
            LogLog.Debug(declaringType, "defaultRepositoryType [" + m_defaultRepositoryType + "]");
        }
 
        #endregion Public Instance Constructors
 
        #region Implementation of IRepositorySelector
 
        /// <summary>
        /// Gets the <see cref="ILoggerRepository"/> for the specified assembly.
        /// </summary>
        /// <param name="repositoryAssembly">The assembly use to lookup the <see cref="ILoggerRepository"/>.</param>
        /// <remarks>
        /// <para>
        /// The type of the <see cref="ILoggerRepository"/> created and the repository 
        /// to create can be overridden by specifying the <see cref="log4net.Config.RepositoryAttribute"/> 
        /// attribute on the <paramref name="repositoryAssembly"/>.
        /// </para>
        /// <para>
        /// The default values are to use the <see cref="log4net.Repository.Hierarchy.Hierarchy"/> 
        /// implementation of the <see cref="ILoggerRepository"/> interface and to use the
        /// <see cref="AssemblyName.Name"/> as the name of the repository.
        /// </para>
        /// <para>
        /// The <see cref="ILoggerRepository"/> created will be automatically configured using 
        /// any <see cref="log4net.Config.ConfiguratorAttribute"/> attributes defined on
        /// the <paramref name="repositoryAssembly"/>.
        /// </para>
        /// </remarks>
        /// <returns>The <see cref="ILoggerRepository"/> for the assembly</returns>
        /// <exception cref="ArgumentNullException"><paramref name="repositoryAssembly"/> is <see langword="null" />.</exception>
        public ILoggerRepository GetRepository(Assembly repositoryAssembly)
        {
            if (repositoryAssembly == null)
            {
                throw new ArgumentNullException("repositoryAssembly");
            }
            return CreateRepository(repositoryAssembly, m_defaultRepositoryType);
        }
 
        /// <summary>
        /// Gets the <see cref="ILoggerRepository"/> for the specified repository.
        /// </summary>
        /// <param name="repositoryName">The repository to use to lookup the <see cref="ILoggerRepository"/>.</param>
        /// <returns>The <see cref="ILoggerRepository"/> for the specified repository.</returns>
        /// <remarks>
        /// <para>
        /// Returns the named repository. If <paramref name="repositoryName"/> is <c>null</c>
        /// a <see cref="ArgumentNullException"/> is thrown. If the repository 
        /// does not exist a <see cref="LogException"/> is thrown.
        /// </para>
        /// <para>
        /// Use <see cref="CreateRepository(string, Type)"/> to create a repository.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="repositoryName"/> is <see langword="null" />.</exception>
        /// <exception cref="LogException"><paramref name="repositoryName"/> does not exist.</exception>
        public ILoggerRepository GetRepository(string repositoryName)
        {
            if (repositoryName == null)
            {
                throw new ArgumentNullException("repositoryName");
            }
 
            lock(this)
            {
                // Lookup in map
                ILoggerRepository rep = m_name2repositoryMap[repositoryName] as ILoggerRepository;
                if (rep == null)
                {
                    throw new LogException("Repository [" + repositoryName + "] is NOT defined.");
                }
                return rep;
            }
        }
 
        /// <summary>
        /// Create a new repository for the assembly specified 
        /// </summary>
        /// <param name="repositoryAssembly">the assembly to use to create the repository to associate with the <see cref="ILoggerRepository"/>.</param>
        /// <param name="repositoryType">The type of repository to create, must implement <see cref="ILoggerRepository"/>.</param>
        /// <returns>The repository created.</returns>
        /// <remarks>
        /// <para>
        /// The <see cref="ILoggerRepository"/> created will be associated with the repository
        /// specified such that a call to <see cref="GetRepository(Assembly)"/> with the
        /// same assembly specified will return the same repository instance.
        /// </para>
        /// <para>
        /// The type of the <see cref="ILoggerRepository"/> created and
        /// the repository to create can be overridden by specifying the
        /// <see cref="log4net.Config.RepositoryAttribute"/> attribute on the 
        /// <paramref name="repositoryAssembly"/>.  The default values are to use the 
        /// <paramref name="repositoryType"/> implementation of the 
        /// <see cref="ILoggerRepository"/> interface and to use the
        /// <see cref="AssemblyName.Name"/> as the name of the repository.
        /// </para>
        /// <para>
        /// The <see cref="ILoggerRepository"/> created will be automatically
        /// configured using any <see cref="log4net.Config.ConfiguratorAttribute"/> 
        /// attributes defined on the <paramref name="repositoryAssembly"/>.
        /// </para>
        /// <para>
        /// If a repository for the <paramref name="repositoryAssembly"/> already exists
        /// that repository will be returned. An error will not be raised and that 
        /// repository may be of a different type to that specified in <paramref name="repositoryType"/>.
        /// Also the <see cref="log4net.Config.RepositoryAttribute"/> attribute on the
        /// assembly may be used to override the repository type specified in 
        /// <paramref name="repositoryType"/>.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="repositoryAssembly"/> is <see langword="null" />.</exception>
        public ILoggerRepository CreateRepository(Assembly repositoryAssembly, Type repositoryType)
        {
            return CreateRepository(repositoryAssembly, repositoryType, DefaultRepositoryName, true);
        }
 
        /// <summary>
        /// Creates a new repository for the assembly specified.
        /// </summary>
        /// <param name="repositoryAssembly">the assembly to use to create the repository to associate with the <see cref="ILoggerRepository"/>.</param>
        /// <param name="repositoryType">The type of repository to create, must implement <see cref="ILoggerRepository"/>.</param>
        /// <param name="repositoryName">The name to assign to the created repository</param>
        /// <param name="readAssemblyAttributes">Set to <c>true</c> to read and apply the assembly attributes</param>
        /// <returns>The repository created.</returns>
        /// <remarks>
        /// <para>
        /// The <see cref="ILoggerRepository"/> created will be associated with the repository
        /// specified such that a call to <see cref="GetRepository(Assembly)"/> with the
        /// same assembly specified will return the same repository instance.
        /// </para>
        /// <para>
        /// The type of the <see cref="ILoggerRepository"/> created and
        /// the repository to create can be overridden by specifying the
        /// <see cref="log4net.Config.RepositoryAttribute"/> attribute on the 
        /// <paramref name="repositoryAssembly"/>.  The default values are to use the 
        /// <paramref name="repositoryType"/> implementation of the 
        /// <see cref="ILoggerRepository"/> interface and to use the
        /// <see cref="AssemblyName.Name"/> as the name of the repository.
        /// </para>
        /// <para>
        /// The <see cref="ILoggerRepository"/> created will be automatically
        /// configured using any <see cref="log4net.Config.ConfiguratorAttribute"/> 
        /// attributes defined on the <paramref name="repositoryAssembly"/>.
        /// </para>
        /// <para>
        /// If a repository for the <paramref name="repositoryAssembly"/> already exists
        /// that repository will be returned. An error will not be raised and that 
        /// repository may be of a different type to that specified in <paramref name="repositoryType"/>.
        /// Also the <see cref="log4net.Config.RepositoryAttribute"/> attribute on the
        /// assembly may be used to override the repository type specified in 
        /// <paramref name="repositoryType"/>.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="repositoryAssembly"/> is <see langword="null" />.</exception>
        public ILoggerRepository CreateRepository(Assembly repositoryAssembly, Type repositoryType, string repositoryName, bool readAssemblyAttributes)
        {
            if (repositoryAssembly == null)
            {
                throw new ArgumentNullException("repositoryAssembly");
            }
 
            // If the type is not set then use the default type
            if (repositoryType == null)
            {
                repositoryType = m_defaultRepositoryType;
            }
 
            lock(this)
            {
                // Lookup in map
                ILoggerRepository rep = m_assembly2repositoryMap[repositoryAssembly] as ILoggerRepository;
                if (rep == null)
                {
                    // Not found, therefore create
                    LogLog.Debug(declaringType, "Creating repository for assembly [" + repositoryAssembly + "]");
 
                    // Must specify defaults
                    string actualRepositoryName = repositoryName;
                    Type actualRepositoryType = repositoryType;
 
                    if (readAssemblyAttributes)
                    {
                        // Get the repository and type from the assembly attributes
                        GetInfoForAssembly(repositoryAssembly, ref actualRepositoryName, ref actualRepositoryType);
                    }
 
                    LogLog.Debug(declaringType, "Assembly [" + repositoryAssembly + "] using repository [" + actualRepositoryName + "] and repository type [" + actualRepositoryType + "]");
 
                    // Lookup the repository in the map (as this may already be defined)
                    rep = m_name2repositoryMap[actualRepositoryName] as ILoggerRepository;
                    if (rep == null)
                    {
                        // Create the repository
                        rep = CreateRepository(actualRepositoryName, actualRepositoryType);
 
                        if (readAssemblyAttributes)
                        {
                            try
                            {
                                // Look for aliasing attributes
                                LoadAliases(repositoryAssembly, rep);
 
                                // Look for plugins defined on the assembly
                                LoadPlugins(repositoryAssembly, rep);
 
                                // Configure the repository using the assembly attributes
                                ConfigureRepository(repositoryAssembly, rep);
                            }
                            catch (Exception ex)
                            {
                                LogLog.Error(declaringType, "Failed to configure repository [" + actualRepositoryName + "] from assembly attributes.", ex);
                            }
                        }
                    }
                    else
                    {
                        LogLog.Debug(declaringType, "repository [" + actualRepositoryName + "] already exists, using repository type [" + rep.GetType().FullName + "]");
 
                        if (readAssemblyAttributes)
                        {
                            try
                            {
                                // Look for plugins defined on the assembly
                                LoadPlugins(repositoryAssembly, rep);
                            }
                            catch (Exception ex)
                            {
                                LogLog.Error(declaringType, "Failed to configure repository [" + actualRepositoryName + "] from assembly attributes.", ex);
                            }
                        }
                    }
                    m_assembly2repositoryMap[repositoryAssembly] = rep;
                }
                return rep;
            }
        }
 
        /// <summary>
        /// Creates a new repository for the specified repository.
        /// </summary>
        /// <param name="repositoryName">The repository to associate with the <see cref="ILoggerRepository"/>.</param>
        /// <param name="repositoryType">The type of repository to create, must implement <see cref="ILoggerRepository"/>.
        /// If this param is <see langword="null" /> then the default repository type is used.</param>
        /// <returns>The new repository.</returns>
        /// <remarks>
        /// <para>
        /// The <see cref="ILoggerRepository"/> created will be associated with the repository
        /// specified such that a call to <see cref="GetRepository(string)"/> with the
        /// same repository specified will return the same repository instance.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="repositoryName"/> is <see langword="null" />.</exception>
        /// <exception cref="LogException"><paramref name="repositoryName"/> already exists.</exception>
        public ILoggerRepository CreateRepository(string repositoryName, Type repositoryType)
        {
            if (repositoryName == null)
            {
                throw new ArgumentNullException("repositoryName");
            }
 
            // If the type is not set then use the default type
            if (repositoryType == null)
            {
                repositoryType = m_defaultRepositoryType;
            }
 
            lock(this)
            {
                ILoggerRepository rep = null;
 
                // First check that the repository does not exist
                rep = m_name2repositoryMap[repositoryName] as ILoggerRepository;
                if (rep != null)
                {
                    throw new LogException("Repository [" + repositoryName + "] is already defined. Repositories cannot be redefined.");
                }
                else
                {
                    // Lookup an alias before trying to create the new repository
                    ILoggerRepository aliasedRepository = m_alias2repositoryMap[repositoryName] as ILoggerRepository;
                    if (aliasedRepository != null)
                    {
                        // Found an alias
 
                        // Check repository type
                        if (aliasedRepository.GetType() == repositoryType)
                        {
                            // Repository type is compatible
                            LogLog.Debug(declaringType, "Aliasing repository [" + repositoryName + "] to existing repository [" + aliasedRepository.Name + "]");
                            rep = aliasedRepository;
 
                            // Store in map
                            m_name2repositoryMap[repositoryName] = rep;
                        }
                        else
                        {
                            // Invalid repository type for alias
                            LogLog.Error(declaringType, "Failed to alias repository [" + repositoryName + "] to existing repository ["+aliasedRepository.Name+"]. Requested repository type ["+repositoryType.FullName+"] is not compatible with existing type [" + aliasedRepository.GetType().FullName + "]");
 
                            // We now drop through to create the repository without aliasing
                        }
                    }
 
                    // If we could not find an alias
                    if (rep == null)
                    {
                        LogLog.Debug(declaringType, "Creating repository [" + repositoryName + "] using type [" + repositoryType + "]");
 
                        // Call the no arg constructor for the repositoryType
                        rep = (ILoggerRepository)Activator.CreateInstance(repositoryType);
 
                        // Set the name of the repository
                        rep.Name = repositoryName;
 
                        // Store in map
                        m_name2repositoryMap[repositoryName] = rep;
 
                        // Notify listeners that the repository has been created
                        OnLoggerRepositoryCreatedEvent(rep);
                    }
                }
 
                return rep;
            }
        }
 
        /// <summary>
        /// Test if a named repository exists
        /// </summary>
        /// <param name="repositoryName">the named repository to check</param>
        /// <returns><c>true</c> if the repository exists</returns>
        /// <remarks>
        /// <para>
        /// Test if a named repository exists. Use <see cref="CreateRepository(string, Type)"/>
        /// to create a new repository and <see cref="GetRepository(string)"/> to retrieve 
        /// a repository.
        /// </para>
        /// </remarks>
        public bool ExistsRepository(string repositoryName)
        {
            lock(this)
            {
                return m_name2repositoryMap.ContainsKey(repositoryName);
            }
        }
 
        /// <summary>
        /// Gets a list of <see cref="ILoggerRepository"/> objects
        /// </summary>
        /// <returns>an array of all known <see cref="ILoggerRepository"/> objects</returns>
        /// <remarks>
        /// <para>
        /// Gets an array of all of the repositories created by this selector.
        /// </para>
        /// </remarks>
        public ILoggerRepository[] GetAllRepositories()
        {
            lock(this)
            {
                ICollection reps = m_name2repositoryMap.Values;
                ILoggerRepository[] all = new ILoggerRepository[reps.Count];
                reps.CopyTo(all, 0);
                return all;
            }
        }
 
        #endregion Implementation of IRepositorySelector
 
        #region Public Instance Methods
 
        /// <summary>
        /// Aliases a repository to an existing repository.
        /// </summary>
        /// <param name="repositoryAlias">The repository to alias.</param>
        /// <param name="repositoryTarget">The repository that the repository is aliased to.</param>
        /// <remarks>
        /// <para>
        /// The repository specified will be aliased to the repository when created. 
        /// The repository must not already exist.
        /// </para>
        /// <para>
        /// When the repository is created it must utilize the same repository type as 
        /// the repository it is aliased to, otherwise the aliasing will fail.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        ///    <para><paramref name="repositoryAlias" /> is <see langword="null" />.</para>
        ///    <para>-or-</para>
        ///    <para><paramref name="repositoryTarget" /> is <see langword="null" />.</para>
        /// </exception>
        public void AliasRepository(string repositoryAlias, ILoggerRepository repositoryTarget) 
        {
            if (repositoryAlias == null) 
            {
                throw new ArgumentNullException("repositoryAlias");
            }
            if (repositoryTarget == null) 
            {
                throw new ArgumentNullException("repositoryTarget");
            }
 
            lock(this) 
            {
                // Check if the alias is already set
                if (m_alias2repositoryMap.Contains(repositoryAlias)) 
                {
                    // Check if this is a duplicate of the current alias
                    if (repositoryTarget != ((ILoggerRepository)m_alias2repositoryMap[repositoryAlias])) 
                    {
                        // Cannot redefine existing alias
                        throw new InvalidOperationException("Repository [" + repositoryAlias + "] is already aliased to repository [" + ((ILoggerRepository)m_alias2repositoryMap[repositoryAlias]).Name + "]. Aliases cannot be redefined.");
                    }
                }
                    // Check if the alias is already mapped to a repository
                else if (m_name2repositoryMap.Contains(repositoryAlias)) 
                {
                    // Check if this is a duplicate of the current mapping
                    if ( repositoryTarget != ((ILoggerRepository)m_name2repositoryMap[repositoryAlias]) ) 
                    {
                        // Cannot define alias for already mapped repository
                        throw new InvalidOperationException("Repository [" + repositoryAlias + "] already exists and cannot be aliased to repository [" + repositoryTarget.Name + "].");
                    }
                }
                else 
                {
                    // Set the alias
                    m_alias2repositoryMap[repositoryAlias] = repositoryTarget;
                }
            }
        }
 
        #endregion Public Instance Methods
 
        #region Protected Instance Methods
 
        /// <summary>
        /// Notifies the registered listeners that the repository has been created.
        /// </summary>
        /// <param name="repository">The repository that has been created.</param>
        /// <remarks>
        /// <para>
        /// Raises the <see cref="LoggerRepositoryCreatedEvent"/> event.
        /// </para>
        /// </remarks>
        protected virtual void OnLoggerRepositoryCreatedEvent(ILoggerRepository repository) 
        {
            LoggerRepositoryCreationEventHandler handler = m_loggerRepositoryCreatedEvent;
            if (handler != null) 
            {
                handler(this, new LoggerRepositoryCreationEventArgs(repository));
            }
        }
 
        #endregion Protected Instance Methods
 
        #region Private Instance Methods
 
        /// <summary>
        /// Gets the repository name and repository type for the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly that has a <see cref="log4net.Config.RepositoryAttribute"/>.</param>
        /// <param name="repositoryName">in/out param to hold the repository name to use for the assembly, caller should set this to the default value before calling.</param>
        /// <param name="repositoryType">in/out param to hold the type of the repository to create for the assembly, caller should set this to the default value before calling.</param>
        /// <exception cref="ArgumentNullException"><paramref name="assembly" /> is <see langword="null" />.</exception>
        private void GetInfoForAssembly(Assembly assembly, ref string repositoryName, ref Type repositoryType)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
 
            try
            {
                LogLog.Debug(declaringType, "Assembly [" + assembly.FullName + "] Loaded From [" + SystemInfo.AssemblyLocationInfo(assembly) + "]");
            }
            catch
            {
                // Ignore exception from debug call
            }
 
            try
            {
                // Look for the RepositoryAttribute on the assembly 
                object[] repositoryAttributes = Attribute.GetCustomAttributes(assembly, typeof(log4net.Config.RepositoryAttribute), false);
                if (repositoryAttributes == null || repositoryAttributes.Length == 0)
                {
                    // This is not a problem, but its nice to know what is going on.
                    LogLog.Debug(declaringType, "Assembly [" + assembly + "] does not have a RepositoryAttribute specified.");
                }
                else
                {
                    if (repositoryAttributes.Length > 1)
                    {
                        LogLog.Error(declaringType, "Assembly [" + assembly + "] has multiple log4net.Config.RepositoryAttribute assembly attributes. Only using first occurrence.");
                    }
 
                    log4net.Config.RepositoryAttribute domAttr = repositoryAttributes[0] as log4net.Config.RepositoryAttribute;
 
                    if (domAttr == null)
                    {
                        LogLog.Error(declaringType, "Assembly [" + assembly + "] has a RepositoryAttribute but it does not!.");
                    }
                    else
                    {
                        // If the Name property is set then override the default
                        if (domAttr.Name != null)
                        {
                            repositoryName = domAttr.Name;
                        }
 
                        // If the RepositoryType property is set then override the default
                        if (domAttr.RepositoryType != null)
                        {
                            // Check that the type is a repository
                            if (typeof(ILoggerRepository).IsAssignableFrom(domAttr.RepositoryType))
                            {
                                repositoryType = domAttr.RepositoryType;
                            }
                            else
                            {
                                LogLog.Error(declaringType, "DefaultRepositorySelector: Repository Type [" + domAttr.RepositoryType + "] must implement the ILoggerRepository interface.");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogLog.Error(declaringType, "Unhandled exception in GetInfoForAssembly", ex);
            }
        }
 
        /// <summary>
        /// Configures the repository using information from the assembly.
        /// </summary>
        /// <param name="assembly">The assembly containing <see cref="log4net.Config.ConfiguratorAttribute"/>
        /// attributes which define the configuration for the repository.</param>
        /// <param name="repository">The repository to configure.</param>
        /// <exception cref="ArgumentNullException">
        ///    <para><paramref name="assembly" /> is <see langword="null" />.</para>
        ///    <para>-or-</para>
        ///    <para><paramref name="repository" /> is <see langword="null" />.</para>
        /// </exception>
        private void ConfigureRepository(Assembly assembly, ILoggerRepository repository)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }
 
            // Look for the Configurator attributes (e.g. XmlConfiguratorAttribute) on the assembly
            object[] configAttributes = Attribute.GetCustomAttributes(assembly, typeof(log4net.Config.ConfiguratorAttribute), false);
            if (configAttributes != null && configAttributes.Length > 0)
            {
                // Sort the ConfiguratorAttributes in priority order
                Array.Sort(configAttributes);
 
                // Delegate to the attribute the job of configuring the repository
                foreach(log4net.Config.ConfiguratorAttribute configAttr in configAttributes)
                {
                    if (configAttr != null)
                    {
                        try
                        {
                            configAttr.Configure(assembly, repository);
                        }
                        catch (Exception ex)
                        {
                            LogLog.Error(declaringType, "Exception calling ["+configAttr.GetType().FullName+"] .Configure method.", ex);
                        }
                    }
                }
            }
 
            if (repository.Name == DefaultRepositoryName)
            {
                // Try to configure the default repository using an AppSettings specified config file
                // Do this even if the repository has been configured (or claims to be), this allows overriding
                // of the default config files etc, if that is required.
 
                string repositoryConfigFile = SystemInfo.GetAppSetting("log4net.Config");
                if (repositoryConfigFile != null && repositoryConfigFile.Length > 0)
                {
                    string applicationBaseDirectory = null;
                    try
                    {
                        applicationBaseDirectory = SystemInfo.ApplicationBaseDirectory;
                    }
                    catch(Exception ex)
                    {
                        LogLog.Warn(declaringType, "Exception getting ApplicationBaseDirectory. appSettings log4net.Config path ["+repositoryConfigFile+"] will be treated as an absolute URI", ex);
                    }
 
                    string repositoryConfigFilePath = repositoryConfigFile;
                    if (applicationBaseDirectory != null)
                    {
                        repositoryConfigFilePath = Path.Combine(applicationBaseDirectory, repositoryConfigFile);
                    }
 
                    // Determine whether to watch the file or not based on an app setting value:
                    bool watchRepositoryConfigFile = false;
#if NET_2_0 || MONO_2_0
                    Boolean.TryParse(SystemInfo.GetAppSetting("log4net.Config.Watch"), out watchRepositoryConfigFile);
#else
                                    {
                                        string watch = SystemInfo.GetAppSetting("log4net.Config.Watch");
                                        if (watch != null && watch.Length > 0)
                                        {
                                            try
                                            {
                                                watchRepositoryConfigFile = Boolean.Parse(watch);
                                            }
                                            catch (FormatException)
                                            {
                                                // simply not a Boolean
                                            }
                                        }
                                    }
#endif
 
                    if (watchRepositoryConfigFile)
                    {
                         // As we are going to watch the config file it is required to resolve it as a 
                         // physical file system path pass that in a FileInfo object to the Configurator
                        FileInfo repositoryConfigFileInfo = null;
                        try
                        {
                            repositoryConfigFileInfo = new FileInfo(repositoryConfigFilePath);
                        }
                        catch (Exception ex)
                        {
                            LogLog.Error(declaringType, "DefaultRepositorySelector: Exception while parsing log4net.Config file physical path [" + repositoryConfigFilePath + "]", ex);
                        }
                        try
                        {
                            LogLog.Debug(declaringType, "Loading and watching configuration for default repository from AppSettings specified Config path [" + repositoryConfigFilePath + "]");
 
                            XmlConfigurator.ConfigureAndWatch(repository, repositoryConfigFileInfo);
                        }
                        catch (Exception ex)
                        {
                            LogLog.Error(declaringType, "DefaultRepositorySelector: Exception calling XmlConfigurator.ConfigureAndWatch method with ConfigFilePath [" + repositoryConfigFilePath + "]", ex);
                        }
                    }
                    else
                    {
                    // As we are not going to watch the config file it is easiest to just resolve it as a 
                    // URI and pass that to the Configurator
                    Uri repositoryConfigUri = null;
                    try
                    {
                        repositoryConfigUri = new Uri(repositoryConfigFilePath);
                    }
                    catch(Exception ex)
                    {
                        LogLog.Error(declaringType, "Exception while parsing log4net.Config file path ["+repositoryConfigFile+"]", ex);
                    }
 
                    if (repositoryConfigUri != null)
                    {
                        LogLog.Debug(declaringType, "Loading configuration for default repository from AppSettings specified Config URI ["+repositoryConfigUri.ToString()+"]");
 
                        try
                        {
                            // TODO: Support other types of configurator
                            XmlConfigurator.Configure(repository, repositoryConfigUri);
                        }
                        catch (Exception ex)
                        {
                            LogLog.Error(declaringType, "Exception calling XmlConfigurator.Configure method with ConfigUri ["+repositoryConfigUri+"]", ex);
                        }
                    }
                    }
                }
            }
        }
 
        /// <summary>
        /// Loads the attribute defined plugins on the assembly.
        /// </summary>
        /// <param name="assembly">The assembly that contains the attributes.</param>
        /// <param name="repository">The repository to add the plugins to.</param>
        /// <exception cref="ArgumentNullException">
        ///    <para><paramref name="assembly" /> is <see langword="null" />.</para>
        ///    <para>-or-</para>
        ///    <para><paramref name="repository" /> is <see langword="null" />.</para>
        /// </exception>
        private void LoadPlugins(Assembly assembly, ILoggerRepository repository)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }
 
            // Look for the PluginAttribute on the assembly
            object[] configAttributes = Attribute.GetCustomAttributes(assembly, typeof(log4net.Config.PluginAttribute), false);
            if (configAttributes != null && configAttributes.Length > 0)
            {
                foreach(log4net.Plugin.IPluginFactory configAttr in configAttributes)
                {
                    try
                    {
                        // Create the plugin and add it to the repository
                        repository.PluginMap.Add(configAttr.CreatePlugin());
                    }
                    catch(Exception ex)
                    {
                        LogLog.Error(declaringType, "Failed to create plugin. Attribute [" + configAttr.ToString() + "]", ex);
                    }
                }
            }
        }
 
        /// <summary>
        /// Loads the attribute defined aliases on the assembly.
        /// </summary>
        /// <param name="assembly">The assembly that contains the attributes.</param>
        /// <param name="repository">The repository to alias to.</param>
        /// <exception cref="ArgumentNullException">
        ///    <para><paramref name="assembly" /> is <see langword="null" />.</para>
        ///    <para>-or-</para>
        ///    <para><paramref name="repository" /> is <see langword="null" />.</para>
        /// </exception>
        private void LoadAliases(Assembly assembly, ILoggerRepository repository)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }
 
            // Look for the AliasRepositoryAttribute on the assembly
            object[] configAttributes = Attribute.GetCustomAttributes(assembly, typeof(log4net.Config.AliasRepositoryAttribute), false);
            if (configAttributes != null && configAttributes.Length > 0)
            {
                foreach(log4net.Config.AliasRepositoryAttribute configAttr in configAttributes)
                {
                    try
                    {
                        AliasRepository(configAttr.Name, repository);
                    }
                    catch(Exception ex)
                    {
                        LogLog.Error(declaringType, "Failed to alias repository [" + configAttr.Name + "]", ex);
                    }
                }
            }
        }
 
        #endregion Private Instance Methods
 
        #region Private Static Fields
 
        /// <summary>
        /// The fully qualified type of the DefaultRepositorySelector class.
        /// </summary>
        /// <remarks>
        /// Used by the internal logger to record the Type of the
        /// log message.
        /// </remarks>
        private readonly static Type declaringType = typeof(DefaultRepositorySelector);
 
        private const string DefaultRepositoryName = "log4net-default-repository";
 
        #endregion Private Static Fields
 
        #region Private Instance Fields
 
        private readonly Hashtable m_name2repositoryMap = new Hashtable();
        private readonly Hashtable m_assembly2repositoryMap = new Hashtable();
        private readonly Hashtable m_alias2repositoryMap = new Hashtable();
        private readonly Type m_defaultRepositoryType;
 
        private event LoggerRepositoryCreationEventHandler m_loggerRepositoryCreatedEvent;
 
        #endregion Private Instance Fields
    }
}
 
#endif // !NETCF