/// <summary>
        /// Create a new repository for the repository specified
        /// </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 null then the default repository type is used.</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="M:GetRepository(string)"/> with the
        /// same repository specified will return the same repository instance.
        /// </para>
        /// <para>
        /// If the named repository already exists an exception will be thrown.
        /// </para>
        /// <para>
        /// If <paramref name="repositoryType"/> is <c>null</c> then the default
        /// repository type specified to the constructor is used.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">throw if <paramref name="repositoryName"/> is null</exception>
        /// <exception cref="LogException">throw if the <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
                {
                    LogLog.Debug(declaringType, "Creating repository [" + repositoryName + "] using type [" + repositoryType + "]");

                    // Call the no arg constructor for the repositoryType
                    rep = (ILoggerRepository)LoggerManager.GetService(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);
            }
        }
예제 #2
0
        /// <summary>
        /// Hook the shutdown event
        /// </summary>
        /// <remarks>
        /// <para>
        /// On the full .NET runtime, the static constructor hooks up the
        /// <c>AppDomain.ProcessExit</c> and <c>AppDomain.DomainUnload</c>> events.
        /// These are used to shutdown the log4net system as the application exits.
        /// </para>
        /// </remarks>
        static LoggerManager()
        {
            try
            {
                // Register the AppDomain events, note we have to do this with a
                // method call rather than directly here because the AppDomain
                // makes a LinkDemand which throws the exception during the JIT phase.
                RegisterAppDomainEvents();
            }
            catch (System.Security.SecurityException)
            {
                LogLog.Debug(declaringType, "Security Exception (ControlAppDomain LinkDemand) while trying " +
                             "to register Shutdown handler with the AppDomain. LoggerManager.Shutdown() " +
                             "will not be called automatically when the AppDomain exits. It must be called " +
                             "programmatically.");
            }

            // Dump out our assembly version into the log if debug is enabled
            LogLog.Debug(declaringType, GetVersionInfo());

            // Set the default repository selector
#if NETCF
            s_repositorySelector = new CompactRepositorySelector(typeof(log4net.Repository.Hierarchy.Hierarchy));
            return;
#elif !NETSTANDARD1_3
            // Look for the RepositorySelector type specified in the AppSettings 'log4net.RepositorySelector'
            string appRepositorySelectorTypeName = SystemInfo.GetAppSetting("log4net.RepositorySelector");
            if (appRepositorySelectorTypeName != null && appRepositorySelectorTypeName.Length > 0)
            {
                // Resolve the config string into a Type
                Type appRepositorySelectorType = null;
                try
                {
                    appRepositorySelectorType = SystemInfo.GetTypeFromString(appRepositorySelectorTypeName, false, true);
                }
                catch (Exception ex)
                {
                    LogLog.Error(declaringType, "Exception while resolving RepositorySelector Type [" + appRepositorySelectorTypeName + "]", ex);
                }

                if (appRepositorySelectorType != null)
                {
                    // Create an instance of the RepositorySelectorType
                    object appRepositorySelectorObj = null;
                    try
                    {
                        appRepositorySelectorObj = LoggerManager.GetService(appRepositorySelectorType);
                    }
                    catch (Exception ex)
                    {
                        LogLog.Error(declaringType, "Exception while creating RepositorySelector [" + appRepositorySelectorType.FullName + "]", ex);
                    }

                    if (appRepositorySelectorObj != null && appRepositorySelectorObj is IRepositorySelector)
                    {
                        s_repositorySelector = (IRepositorySelector)appRepositorySelectorObj;
                    }
                    else
                    {
                        LogLog.Error(declaringType, "RepositorySelector Type [" + appRepositorySelectorType.FullName + "] is not an IRepositorySelector");
                    }
                }
            }
#endif
            // Create the DefaultRepositorySelector if not configured above
            if (s_repositorySelector == null)
            {
                s_repositorySelector = new DefaultRepositorySelector(typeof(log4net.Repository.Hierarchy.Hierarchy));
            }
        }
예제 #3
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)LoggerManager.GetService(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);
            }
        }