Example #1
0
        public static void ChangeFileName(string loggerName, string fileName)
        {
            ILoggerRepository repository = Log4netLoggerManager.GetRepository(DefaultRepository);

            IAppender[] appenders   = repository.GetAppenders();
            var         targetApder = appenders.First(p => p.Name == loggerName) as RollingFileAppender;

            targetApder.File   = fileName;
            targetApder.Writer = new StreamWriter(targetApder.File, targetApder.AppendToFile, targetApder.Encoding);
            //targetApder.ActivateOptions();
        }
Example #2
0
 /// <summary>
 /// Creates a logger with the given logger name.
 /// </summary>
 /// <param name="loggerName">The name of Logger which will be created.</param>
 /// <returns>An instance of <c>ILogger</c>.</returns>
 private static ILogger GetLog4netLogger(string loggerName)
 {
     return(Log4netLoggerManager.GetLogger(Assembly.GetExecutingAssembly(), loggerName));
 }
Example #3
0
        /// <summary>
        /// Load the config file. This method should be called before using LoggerManager.
        /// Only need to be call once.
        /// </summary>
        /// <param name="filePath">The path of the config file.</param>
        public static void Initialize(string filePath)
        {
            FileInfo fileInfo = new FileInfo(Environment.CurrentDirectory + filePath);

            XmlConfigurator.ConfigureAndWatch(Log4netLoggerManager.GetRepository(Assembly.GetExecutingAssembly()), fileInfo);
        }
Example #4
0
 public static ILogger GetLog4netLogger(string loggerName)
 {
     return(Log4netLoggerManager.GetLogger(DefaultRepository, loggerName));
 }
Example #5
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);
            }
        }