1
#region Copyright & License
3
// Copyright 2001-2005 The Apache Software Foundation
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
20
using System.Collections;
21
using System.Reflection;
23
using log4net.Appender;
25
using log4net.Repository;
28
namespace log4net.Core
31
/// The implementation of the <see cref="IRepositorySelector"/> interface suitable
32
/// for use with the compact framework
36
/// This <see cref="IRepositorySelector"/> implementation is a simple
37
/// mapping between repository name and <see cref="ILoggerRepository"/>
41
/// The .NET Compact Framework 1.0 does not support retrieving assembly
42
/// level attributes therefore unlike the <c>DefaultRepositorySelector</c>
43
/// this selector does not examine the calling assembly for attributes.
46
/// <author>Nicko Cadell</author>
47
public class CompactRepositorySelector : IRepositorySelector
49
#region Member Variables
51
private const string DefaultRepositoryName = "log4net-default-repository";
53
private readonly Hashtable m_name2repositoryMap = new Hashtable();
54
private readonly Type m_defaultRepositoryType;
56
private event LoggerRepositoryCreationEventHandler m_loggerRepositoryCreatedEvent;
63
/// Create a new repository selector
65
/// <param name="defaultRepositoryType">the type of the repositories to create, must implement <see cref="ILoggerRepository"/></param>
68
/// Create an new compact repository selector.
69
/// The default type for repositories must be specified,
70
/// an appropriate value would be <see cref="log4net.Repository.Hierarchy.Hierarchy"/>.
73
/// <exception cref="ArgumentNullException">throw if <paramref name="defaultRepositoryType"/> is null</exception>
74
/// <exception cref="ArgumentOutOfRangeException">throw if <paramref name="defaultRepositoryType"/> does not implement <see cref="ILoggerRepository"/></exception>
75
public CompactRepositorySelector(Type defaultRepositoryType)
77
if (defaultRepositoryType == null)
79
throw new ArgumentNullException("defaultRepositoryType");
82
// Check that the type is a repository
83
if (! (typeof(ILoggerRepository).IsAssignableFrom(defaultRepositoryType)) )
85
throw log4net.Util.SystemInfo.CreateArgumentOutOfRangeException("defaultRepositoryType", (object)defaultRepositoryType, "Parameter: defaultRepositoryType, Value: ["+defaultRepositoryType+"] out of range. Argument must implement the ILoggerRepository interface");
88
m_defaultRepositoryType = defaultRepositoryType;
90
LogLog.Debug("CompactRepositorySelector: defaultRepositoryType ["+m_defaultRepositoryType+"]");
95
#region Implementation of IRepositorySelector
98
/// Get the <see cref="ILoggerRepository"/> for the specified assembly
100
/// <param name="assembly">not used</param>
101
/// <returns>The default <see cref="ILoggerRepository"/></returns>
104
/// The <paramref name="assembly"/> argument is not used. This selector does not create a
105
/// separate repository for each assembly.
108
/// As a named repository is not specified the default repository is
109
/// returned. The default repository is named <c>log4net-default-repository</c>.
112
public ILoggerRepository GetRepository(Assembly assembly)
114
return CreateRepository(assembly, m_defaultRepositoryType);
118
/// Get the named <see cref="ILoggerRepository"/>
120
/// <param name="repositoryName">the name of the repository to lookup</param>
121
/// <returns>The named <see cref="ILoggerRepository"/></returns>
124
/// Get the named <see cref="ILoggerRepository"/>. The default
125
/// repository is <c>log4net-default-repository</c>. Other repositories
126
/// must be created using the <see cref="CreateRepository(string, Type)"/>.
127
/// If the named repository does not exist an exception is thrown.
130
/// <exception cref="ArgumentNullException">throw if <paramref name="repositoryName"/> is null</exception>
131
/// <exception cref="LogException">throw if the <paramref name="repositoryName"/> does not exist</exception>
132
public ILoggerRepository GetRepository(string repositoryName)
134
if (repositoryName == null)
136
throw new ArgumentNullException("repositoryName");
142
ILoggerRepository rep = m_name2repositoryMap[repositoryName] as ILoggerRepository;
145
throw new LogException("Repository ["+repositoryName+"] is NOT defined.");
152
/// Create a new repository for the assembly specified
154
/// <param name="assembly">not used</param>
155
/// <param name="repositoryType">the type of repository to create, must implement <see cref="ILoggerRepository"/></param>
156
/// <returns>the repository created</returns>
159
/// The <paramref name="assembly"/> argument is not used. This selector does not create a
160
/// separate repository for each assembly.
163
/// If the <paramref name="repositoryType"/> is <c>null</c> then the
164
/// default repository type specified to the constructor is used.
167
/// As a named repository is not specified the default repository is
168
/// returned. The default repository is named <c>log4net-default-repository</c>.
171
public ILoggerRepository CreateRepository(Assembly assembly, Type repositoryType)
173
// If the type is not set then use the default type
174
if (repositoryType == null)
176
repositoryType = m_defaultRepositoryType;
181
// This method should not throw if the default repository already exists.
183
// First check that the repository does not exist
184
ILoggerRepository rep = m_name2repositoryMap[DefaultRepositoryName] as ILoggerRepository;
187
// Must create the repository
188
rep = CreateRepository(DefaultRepositoryName, repositoryType);
196
/// Create a new repository for the repository specified
198
/// <param name="repositoryName">the repository to associate with the <see cref="ILoggerRepository"/></param>
199
/// <param name="repositoryType">the type of repository to create, must implement <see cref="ILoggerRepository"/>.
200
/// If this param is null then the default repository type is used.</param>
201
/// <returns>the repository created</returns>
204
/// The <see cref="ILoggerRepository"/> created will be associated with the repository
205
/// specified such that a call to <see cref="GetRepository(string)"/> with the
206
/// same repository specified will return the same repository instance.
209
/// If the named repository already exists an exception will be thrown.
212
/// If <paramref name="repositoryType"/> is <c>null</c> then the default
213
/// repository type specified to the constructor is used.
216
/// <exception cref="ArgumentNullException">throw if <paramref name="repositoryName"/> is null</exception>
217
/// <exception cref="LogException">throw if the <paramref name="repositoryName"/> already exists</exception>
218
public ILoggerRepository CreateRepository(string repositoryName, Type repositoryType)
220
if (repositoryName == null)
222
throw new ArgumentNullException("repositoryName");
225
// If the type is not set then use the default type
226
if (repositoryType == null)
228
repositoryType = m_defaultRepositoryType;
233
ILoggerRepository rep = null;
235
// First check that the repository does not exist
236
rep = m_name2repositoryMap[repositoryName] as ILoggerRepository;
239
throw new LogException("Repository ["+repositoryName+"] is already defined. Repositories cannot be redefined.");
243
LogLog.Debug("DefaultRepositorySelector: Creating repository ["+repositoryName+"] using type ["+repositoryType+"]");
245
// Call the no arg constructor for the repositoryType
246
rep = (ILoggerRepository)Activator.CreateInstance(repositoryType);
248
// Set the name of the repository
249
rep.Name = repositoryName;
252
m_name2repositoryMap[repositoryName] = rep;
254
// Notify listeners that the repository has been created
255
OnLoggerRepositoryCreatedEvent(rep);
263
/// Test if a named repository exists
265
/// <param name="repositoryName">the named repository to check</param>
266
/// <returns><c>true</c> if the repository exists</returns>
269
/// Test if a named repository exists. Use <see cref="CreateRepository(string, Type)"/>
270
/// to create a new repository and <see cref="GetRepository(string)"/> to retrieve
274
public bool ExistsRepository(string repositoryName)
278
return m_name2repositoryMap.ContainsKey(repositoryName);
283
/// Gets a list of <see cref="ILoggerRepository"/> objects
285
/// <returns>an array of all known <see cref="ILoggerRepository"/> objects</returns>
288
/// Gets an array of all of the repositories created by this selector.
291
public ILoggerRepository[] GetAllRepositories()
295
ICollection reps = m_name2repositoryMap.Values;
296
ILoggerRepository[] all = new ILoggerRepository[reps.Count];
305
/// Event to notify that a logger repository has been created.
308
/// Event to notify that a logger repository has been created.
312
/// Event raised when a new repository is created.
313
/// The event source will be this selector. The event args will
314
/// be a <see cref="LoggerRepositoryCreationEventArgs"/> which
315
/// holds the newly created <see cref="ILoggerRepository"/>.
318
public event LoggerRepositoryCreationEventHandler LoggerRepositoryCreatedEvent
320
add { m_loggerRepositoryCreatedEvent += value; }
321
remove { m_loggerRepositoryCreatedEvent -= value; }
325
/// Notify the registered listeners that the repository has been created
327
/// <param name="repository">The repository that has been created</param>
330
/// Raises the <event cref="LoggerRepositoryCreatedEvent">LoggerRepositoryCreatedEvent</event>
334
protected virtual void OnLoggerRepositoryCreatedEvent(ILoggerRepository repository)
336
LoggerRepositoryCreationEventHandler handler = m_loggerRepositoryCreatedEvent;
339
handler(this, new LoggerRepositoryCreationEventArgs(repository));