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
#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 System.Reflection;
 
using log4net.Appender;
using log4net.Layout;
using log4net.Util;
using log4net.Repository;
using log4net.Repository.Hierarchy;
 
namespace log4net.Config
{
    /// <summary>
    /// Use this class to quickly configure a <see cref="Hierarchy"/>.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Allows very simple programmatic configuration of log4net.
    /// </para>
    /// <para>
    /// Only one appender can be configured using this configurator.
    /// The appender is set at the root of the hierarchy and all logging
    /// events will be delivered to that appender.
    /// </para>
    /// <para>
    /// Appenders can also implement the <see cref="log4net.Core.IOptionHandler"/> interface. Therefore
    /// they would require that the <see cref="log4net.Core.IOptionHandler.ActivateOptions()"/> method
    /// be called after the appenders properties have been configured.
    /// </para>
    /// </remarks>
    /// <author>Nicko Cadell</author>
    /// <author>Gert Driesen</author>
    public sealed class BasicConfigurator
    {
        #region Private Static Fields
 
        /// <summary>
        /// The fully qualified type of the BasicConfigurator class.
        /// </summary>
        /// <remarks>
        /// Used by the internal logger to record the Type of the
        /// log message.
        /// </remarks>
        private readonly static Type declaringType = typeof(BasicConfigurator);
 
        #endregion Private Static Fields
 
        #region Private Instance Constructors
 
        /// <summary>
        /// Initializes a new instance of the <see cref="BasicConfigurator" /> class. 
        /// </summary>
        /// <remarks>
        /// <para>
        /// Uses a private access modifier to prevent instantiation of this class.
        /// </para>
        /// </remarks>
        private BasicConfigurator()
        {
        }
 
        #endregion Private Instance Constructors
 
        #region Public Static Methods
 
        /// <summary>
        /// Initializes the log4net system with a default configuration.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Initializes the log4net logging system using a <see cref="ConsoleAppender"/>
        /// that will write to <c>Console.Out</c>. The log messages are
        /// formatted using the <see cref="PatternLayout"/> layout object
        /// with the <see cref="PatternLayout.DetailConversionPattern"/>
        /// layout style.
        /// </para>
        /// </remarks>
        static public ICollection Configure()
        {
            return BasicConfigurator.Configure(LogManager.GetRepository(Assembly.GetCallingAssembly()));
        }
 
        /// <summary>
        /// Initializes the log4net system using the specified appender.
        /// </summary>
        /// <param name="appender">The appender to use to log all logging events.</param>
        /// <remarks>
        /// <para>
        /// Initializes the log4net system using the specified appender.
        /// </para>
        /// </remarks>
        static public ICollection Configure(IAppender appender) 
        {
            return Configure(new IAppender[] { appender });
        }
 
        /// <summary>
        /// Initializes the log4net system using the specified appenders.
        /// </summary>
        /// <param name="appenders">The appenders to use to log all logging events.</param>
        /// <remarks>
        /// <para>
        /// Initializes the log4net system using the specified appenders.
        /// </para>
        /// </remarks>
        static public ICollection Configure(params IAppender[] appenders)
        {
            ArrayList configurationMessages = new ArrayList();
 
            ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly());
 
            using (new LogLog.LogReceivedAdapter(configurationMessages))
            {
                InternalConfigure(repository, appenders);
            }
 
            repository.ConfigurationMessages = configurationMessages;
 
            return configurationMessages;
        }
 
        /// <summary>
        /// Initializes the <see cref="ILoggerRepository"/> with a default configuration.
        /// </summary>
        /// <param name="repository">The repository to configure.</param>
        /// <remarks>
        /// <para>
        /// Initializes the specified repository using a <see cref="ConsoleAppender"/>
        /// that will write to <c>Console.Out</c>. The log messages are
        /// formatted using the <see cref="PatternLayout"/> layout object
        /// with the <see cref="PatternLayout.DetailConversionPattern"/>
        /// layout style.
        /// </para>
        /// </remarks>
        static public ICollection Configure(ILoggerRepository repository) 
        {
            ArrayList configurationMessages = new ArrayList();
 
            using (new LogLog.LogReceivedAdapter(configurationMessages))
            {
                // Create the layout
                PatternLayout layout = new PatternLayout();
                layout.ConversionPattern = PatternLayout.DetailConversionPattern;
                layout.ActivateOptions();
 
                // Create the appender
                ConsoleAppender appender = new ConsoleAppender();
                appender.Layout = layout;
                appender.ActivateOptions();
 
                InternalConfigure(repository, appender);
            }
 
            repository.ConfigurationMessages = configurationMessages;
 
            return configurationMessages;
        }
 
        /// <summary>
        /// Initializes the <see cref="ILoggerRepository"/> using the specified appender.
        /// </summary>
        /// <param name="repository">The repository to configure.</param>
        /// <param name="appender">The appender to use to log all logging events.</param>
        /// <remarks>
        /// <para>
        /// Initializes the <see cref="ILoggerRepository"/> using the specified appender.
        /// </para>
        /// </remarks>
        static public ICollection Configure(ILoggerRepository repository, IAppender appender)
        {
            return Configure(repository, new IAppender[] { appender });
        }
 
        /// <summary>
        /// Initializes the <see cref="ILoggerRepository"/> using the specified appenders.
        /// </summary>
        /// <param name="repository">The repository to configure.</param>
        /// <param name="appenders">The appenders to use to log all logging events.</param>
        /// <remarks>
        /// <para>
        /// Initializes the <see cref="ILoggerRepository"/> using the specified appender.
        /// </para>
        /// </remarks>
        static public ICollection Configure(ILoggerRepository repository, params IAppender[] appenders)
        {
            ArrayList configurationMessages = new ArrayList();
 
            using (new LogLog.LogReceivedAdapter(configurationMessages))
            {
                InternalConfigure(repository, appenders);
            }
 
            repository.ConfigurationMessages = configurationMessages;
 
            return configurationMessages;
        }
        
        static private void InternalConfigure(ILoggerRepository repository, params IAppender[] appenders) 
        {
            IBasicRepositoryConfigurator configurableRepository = repository as IBasicRepositoryConfigurator;
            if (configurableRepository != null)
            {
                configurableRepository.Configure(appenders);
            }
            else
            {
                LogLog.Warn(declaringType, "BasicConfigurator: Repository [" + repository + "] does not support the BasicConfigurator");
            }
        }
 
        #endregion Public Static Methods
    }
}