defaultrepositoryselector.cs
来自「SharpDevelop2.0.0 c#开发免费工具」· CS 代码 · 共 743 行 · 第 1/2 页
CS
743 行
if (aliasedRepository.GetType() == repositoryType)
{
// Repository type is compatible
LogLog.Debug("DefaultRepositorySelector: 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("DefaultRepositorySelector: 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("DefaultRepositorySelector: 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"/>
/// to create a new repository and <see cref="GetRepository"/> 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");
}
LogLog.Debug("DefaultRepositorySelector: Assembly [" + assembly.FullName + "] Loaded From [" + SystemInfo.AssemblyLocationInfo(assembly) + "]");
// 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("DefaultRepositorySelector: Assembly [" + assembly + "] does not have a RepositoryAttribute specified.");
}
else
{
if (repositoryAttributes.Length > 1)
{
LogLog.Error("DefaultRepositorySelector: 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("DefaultRepositorySelector: 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("DefaultRepositorySelector: Repository Type [" + domAttr.RepositoryType + "] must implement the ILoggerRepository interface.");
}
}
}
}
}
/// <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. DOMConfiguratorAttribute) 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)
{
configAttr.Configure(assembly, repository);
}
}
}
/// <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("DefaultRepositorySelector: 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("DefaultRepositorySelector: Failed to alias repository [" + configAttr.Name + "]", ex);
}
}
}
}
#endregion Private Instance Methods
#region Private Static Fields
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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?