Пример #1
0
        /// <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);
            }
        }
Пример #2
0
        /// <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="M: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);
            }
        }
Пример #3
0
        /// <summary>
        /// Create a new repository for the domain specified
        /// </summary>
        /// <param name="domain">the domain 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 null then the default repository type is used.</param>
        /// <returns>the repository created</returns>
        /// <remarks>
        /// The <see cref="ILoggerRepository"/> created will be associated with the domain
        /// specified such that a call to <see cref="GetRepository(string)"/> with the
        /// same domain specified will return the same repository instance.
        /// </remarks>
        /// <exception cref="ArgumentNullException">throw if <paramref name="domain"/> is null</exception>
        /// <exception cref="LogException">throw if the <paramref name="domain"/> already exists</exception>
        public ILoggerRepository CreateRepository(string domain, Type repositoryType)
        {
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }

            // 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 domain does not exist
                rep = m_domain2repositoryMap[domain] as ILoggerRepository;
                if (rep != null)
                {
                    throw new LogException("Domain [" + domain + "] is already defined. Domains cannot be redefined.");
                }
                else
                {
                    // Lookup an alias before trying to create the new domain
                    ILoggerRepository aliasedRepository = m_alias2repositoryMap[domain] as ILoggerRepository;
                    if (aliasedRepository != null)
                    {
                        // Found an alias

                        // Check repository type
                        if (aliasedRepository.GetType() == repositoryType)
                        {
                            // Repository type is compatable
                            LogLog.Debug("DefaultRepositorySelector: Aliasing domain [" + domain + "] to existing repository [" + aliasedRepository.Name + "]");
                            rep = aliasedRepository;

                            // Store in map
                            m_domain2repositoryMap[domain] = rep;
                        }
                        else
                        {
                            // Invalid repository type for alias
                            LogLog.Error("DefaultRepositorySelector: Failed to alias domain [" + domain + "] to existing repository [" + aliasedRepository.Name + "]. Requested repository type [" + repositoryType.FullName + "] is not compatable with existing type [" + aliasedRepository.GetType().FullName + "]");

                            // We now drop through to create the domain without aliasing
                        }
                    }

                    // If we could not find an alias
                    if (rep == null)
                    {
                        LogLog.Debug("DefaultRepositorySelector: Creating repository for domain [" + domain + "] using type [" + repositoryType + "]");

                        // Call the no arg constructor for the repositoryType
                        rep = (ILoggerRepository)repositoryType.GetConstructor(SystemInfo.EmptyTypes).Invoke(BindingFlags.Public | BindingFlags.Instance, null, new object[0], CultureInfo.InvariantCulture);

                        // Set the name of the repository
                        rep.Name = domain;

                        // Store in map
                        m_domain2repositoryMap[domain] = rep;

                        // Notify listeners that the repository has been created
                        FireLoggerRepositoryCreatedEvent(rep);
                    }
                }

                return(rep);
            }
        }
Пример #4
0
 /// <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="T:log4net.Repository.ILoggerRepository" />.</param>
 /// <param name="repositoryType">The type of repository to create, must implement <see cref="T:log4net.Repository.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="T:log4net.Repository.ILoggerRepository" /> created will be associated with the repository
 /// specified such that a call to <see cref="M:GetRepository(Assembly)" /> with the
 /// same assembly specified will return the same repository instance.
 /// </para>
 /// <para>
 /// The type of the <see cref="T:log4net.Repository.ILoggerRepository" /> created and
 /// the repository to create can be overridden by specifying the
 /// <see cref="T:log4net.Config.RepositoryAttribute" /> attribute on the
 /// <paramref name="repositoryAssembly" />.  The default values are to use the
 /// <paramref name="repositoryType" /> implementation of the
 /// <see cref="T:log4net.Repository.ILoggerRepository" /> interface and to use the
 /// <see cref="P:System.Reflection.AssemblyName.Name" /> as the name of the repository.
 /// </para>
 /// <para>
 /// The <see cref="T:log4net.Repository.ILoggerRepository" /> created will be automatically
 /// configured using any <see cref="T: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="T:log4net.Config.RepositoryAttribute" /> attribute on the
 /// assembly may be used to override the repository type specified in
 /// <paramref name="repositoryType" />.
 /// </para>
 /// </remarks>
 /// <exception cref="T:System.ArgumentNullException"><paramref name="repositoryAssembly" /> is <see langword="null" />.</exception>
 public ILoggerRepository CreateRepository(Assembly repositoryAssembly, Type repositoryType, string repositoryName, bool readAssemblyAttributes)
 {
     if ((object)repositoryAssembly == null)
     {
         throw new ArgumentNullException("repositoryAssembly");
     }
     if ((object)repositoryType == null)
     {
         repositoryType = m_defaultRepositoryType;
     }
     lock (this)
     {
         ILoggerRepository loggerRepository = m_assembly2repositoryMap[repositoryAssembly] as ILoggerRepository;
         if (loggerRepository == null)
         {
             LogLog.Debug(declaringType, "Creating repository for assembly [" + repositoryAssembly + "]");
             string repositoryName2 = repositoryName;
             Type   repositoryType2 = repositoryType;
             if (readAssemblyAttributes)
             {
                 GetInfoForAssembly(repositoryAssembly, ref repositoryName2, ref repositoryType2);
             }
             LogLog.Debug(declaringType, "Assembly [" + repositoryAssembly + "] using repository [" + repositoryName2 + "] and repository type [" + repositoryType2 + "]");
             loggerRepository = (m_name2repositoryMap[repositoryName2] as ILoggerRepository);
             if (loggerRepository == null)
             {
                 loggerRepository = CreateRepository(repositoryName2, repositoryType2);
                 if (readAssemblyAttributes)
                 {
                     try
                     {
                         LoadAliases(repositoryAssembly, loggerRepository);
                         LoadPlugins(repositoryAssembly, loggerRepository);
                         ConfigureRepository(repositoryAssembly, loggerRepository);
                     }
                     catch (Exception exception)
                     {
                         LogLog.Error(declaringType, "Failed to configure repository [" + repositoryName2 + "] from assembly attributes.", exception);
                     }
                 }
             }
             else
             {
                 LogLog.Debug(declaringType, "repository [" + repositoryName2 + "] already exists, using repository type [" + loggerRepository.GetType().FullName + "]");
                 if (readAssemblyAttributes)
                 {
                     try
                     {
                         LoadPlugins(repositoryAssembly, loggerRepository);
                     }
                     catch (Exception exception2)
                     {
                         LogLog.Error(declaringType, "Failed to configure repository [" + repositoryName2 + "] from assembly attributes.", exception2);
                     }
                 }
             }
             m_assembly2repositoryMap[repositoryAssembly] = loggerRepository;
         }
         return(loggerRepository);
     }
 }
Пример #5
0
        /// <summary>
        /// Create a new repository for the assembly specified
        /// </summary>
        /// <param name="domainAssembly">the assembly to use to create the domain 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="domainName">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 domain
        /// 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 domain to create can be overridden by specifying the
        /// <see cref="log4net.Config.DomainAttribute"/> attribute on the
        /// <paramref name="assembly"/>.  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 domain.
        /// </para>
        /// <para>
        /// The <see cref="ILoggerRepository"/> created will be automatically
        /// configured using any <see cref="log4net.Config.ConfiguratorAttribute"/>
        /// attributes defined on the <paramref name="domainAssembly"/>.
        /// </para>
        /// <para>
        /// If a repository for the <paramref name="domainAssembly"/> 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.DomainAttribute"/> attribute on the
        /// assembly may be used to override the repository type specified in
        /// <paramref name="repositoryType"/>.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="domainAssembly"/> is null.</exception>
        public ILoggerRepository CreateRepository(Assembly domainAssembly, Type repositoryType, string domainName, bool readAssemblyAttributes)
        {
            if (domainAssembly == null)
            {
                throw new ArgumentNullException("domainAssembly");
            }

            // 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[domainAssembly] as ILoggerRepository;
                if (rep == null)
                {
                    // Not found, therefore create
                    LogLog.Debug("DefaultRepositorySelector: Creating repository for assembly [" + domainAssembly + "]");

                    // Must specify defaults
                    string domain = domainName;
                    Type   actualRepositoryType = repositoryType;

                    if (readAssemblyAttributes)
                    {
                        // Get the domain and type from the assembly attributes
                        GetInfoForAssembly(domainAssembly, ref domain, ref actualRepositoryType);
                    }

                    LogLog.Debug("DefaultRepositorySelector: Assembly [" + domainAssembly + "] using domain [" + domain + "] and repository type [" + actualRepositoryType + "]");

                    // Lookup the domain in the map (as this may already be defined)
                    rep = m_domain2repositoryMap[domain] as ILoggerRepository;
                    if (rep == null)
                    {
                        // Create the repository
                        rep = CreateRepository(domain, actualRepositoryType);

                        if (readAssemblyAttributes)
                        {
                            // Look for aliasing attributes
                            LoadAliases(domainAssembly, rep);

                            // Look for plugins defined on the assembly
                            LoadPlugins(domainAssembly, rep);

                            // Configure the repository using the assembly attributes
                            ConfigureRepository(domainAssembly, rep);
                        }
                    }
                    else
                    {
                        LogLog.Debug("DefaultRepositorySelector: domain [" + domain + "] already exisits, using repository type [" + rep.GetType().FullName + "]");

                        if (readAssemblyAttributes)
                        {
                            // Look for plugins defined on the assembly
                            LoadPlugins(domainAssembly, rep);
                        }
                    }
                    m_assembly2repositoryMap[domainAssembly] = rep;
                }
                return(rep);
            }
        }