Exemplo n.º 1
0
        private static void ChangeLog4netLogFileName(string fileName)
        {
            try
            {
                log4net.Core.LogImpl logImpl = ext_log as log4net.Core.LogImpl;
                if (logImpl != null)
                {
                    log4net.Appender.AppenderCollection ac = ((log4net.Repository.Hierarchy.Logger)logImpl.Logger).Appenders;
                    for (int i = 0; i < ac.Count; i++)
                    {
                        // 这里我只对RollingFileAppender类型做修改
                        log4net.Appender.RollingFileAppender rfa = ac[i] as log4net.Appender.RollingFileAppender;
                        if (rfa != null)
                        {
                            fileName = AppDomain.CurrentDomain.BaseDirectory + "\\log\\" + DateTime.Now.ToString("yyyyMM\\\\dd") + "\\" + fileName + ".log";

                            if (rfa.File != null)
                            {
                                if (rfa.File == fileName)
                                {
                                    continue;
                                }
                            }
                            rfa.File = fileName;
                            // 更新Writer属性
                            rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding);
                        }
                    }
                }
            }
            catch
            { }
            ext_log = log4net.LogManager.GetLogger("ext");
        }
Exemplo n.º 2
0
        static log4net.ILog GetLogImpl(string name)
        {
            log4net.Core.LogImpl _log = log4net.LogManager.GetLogger(name) as log4net.Core.LogImpl;
            if (_log != null)
            {
                log4net.Repository.Hierarchy.Logger _logimpl = _log.Logger as log4net.Repository.Hierarchy.Logger;
                if (_logimpl != null)
                {
                    log4net.Repository.Hierarchy.Logger _logger = (log4net.Repository.Hierarchy.Logger)_logimpl;
                    if (_logger.Appenders.Count == 0)
                    {
                        lock (m_lockObj)
                        {
                            if (_logger.Appenders.Count == 0)
                            {
                                _logger.AddAppender(GetFileAppender(name, log4net.Core.Level.Off, log4net.Core.Level.All));
                                _logger.AddAppender(m_allAppender);
                                _logger.AddAppender(m_errorAppender);
                                _logger.AddAppender(m_infoAppender);
                                _logger.AddAppender(m_debugAppender);
                                _logger.AddAppender(m_warnAppender);
                                _logger.AddAppender(m_fatalAppender);

                                //log4net.Repository.Hierarchy.Hierarchy hier = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetAllRepositories()[0];
                                //hier.GetAppen
                                //log4net.Config.XmlConfigurator.Configure(//.BasicConfigurator.Configure(_appender);
                            }
                        }
                    }
                }
            }
            //缓存
            //m_logger.Set(name, _log, m_cachePolicy);
            return(_log);
        }
Exemplo n.º 3
0
        private static void ChangeLog4netLogFileName(log4net.ILog iLog, string fileName)
        {
            log4net.Core.LogImpl logImpl = iLog as log4net.Core.LogImpl;
            if (logImpl != null)
            {
                log4net.Appender.AppenderCollection ac = ((log4net.Repository.Hierarchy.Logger)logImpl.Logger).Appenders;
                for (int i = 0; i < ac.Count; i++)
                {
                    log4net.Appender.RollingFileAppender rfa = ac[i] as log4net.Appender.RollingFileAppender;
                    if (rfa != null)
                    {
                        rfa.File = fileName;
                        if (!System.IO.File.Exists(fileName))
                        {
                            System.IO.File.Create(fileName);
                        }

                        rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding);
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 私有架造方法
        /// </summary>

        static Log4Logger()
        {
            try
            {
                _logConfig = System.Web.HttpContext.Current.Server.MapPath("/Configs/log4net.config");
            }
            catch (NullReferenceException)
            {
                try
                {
                    _logConfig = System.Web.HttpRuntime.AppDomainAppPath + "\\Configs\\log4net.config"; //例如:c:\\project\\
                }
                catch (ArgumentNullException)
                {
                    _logConfig = Environment.CurrentDirectory + "\\Configs\\log4net.config"; //例如:c:\\project\\bin\\debug
                }
            }
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(_logConfig));//初始化指定配置文件
            //log4net.Config.XmlConfigurator.Configure();//初始化web.config里的log4net节点

            logImpl = log4net.LogManager.GetLogger("Core.Logger") as log4net.Core.LogImpl;//Core.Logger为log4net方案名称,为空表示使用root节点
        }