log4net 1.2.10 C#的配置

乱了2天,查了N多牛人的代码,动态调试,终于把log4net1.2.10的配置基本搞定了

我想以下方法,对于我基本够用了

1、动态设置日志文件名称
        public static log4net.ILog log ;
        ///


        /// 动态生成Log日志的名称
        ///

        /// ilog对象
        /// 日志的前缀
        public static void ChangeLogFileName(string fileName)
        {
            log4net.Core.LogImpl logImpl = (log4net.Core.LogImpl)log;
            if (logImpl != null)
            {
                log4net.Appender.AppenderCollection ac = ((log4net.Repository.Hierarchy.Hierarchy)(((log4net.Core.LoggerWrapperImpl)(log)).Logger.Repository)).Root.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/" + fileName + DateTime.Now.ToString("yyyyMMdd") + ".log";
                        rfa.File = fileName;
                        if (!System.IO.File.Exists(fileName))
                        {
                            System.IO.File.AppendAllText(fileName, "");
                        }

                        //更新Writer属性
                        rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding);
                    }
                }
            }
        }

        public static void Error(string Message, Exception ex)
        {
            log.Error(Message, ex);
        }

        public static void Error(string Message)
        {
            log.Error(Message);
        }

        public static void Info(string Message, Exception ex)
        {
            log.Info(Message, ex);
        }
        public static void Info(string Message)
        {
            log.Info(Message);
        }

        public static void Warn(string Message, Exception ex)
        {
            log.Warn(Message, ex);
        }

        public static void Warn(string Message)
        {
            log.Warn(Message);
        }

2、使用其他的配置文件

首先在assembly.cs,添加assembly

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

然后在Program.cs,添加代码

            //注意这里不写全路径可能导致无法找到配置文件
            FileInfo file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            if (File.Exists(file.ToString()))
            {
                log4net.Config.XmlConfigurator.Configure(file);
            }
            else
            {
                throw new Exception("log4net日志配置文件未找到");
            }

这一过程我曾使用log4net.xml来代替log4net.config结果,失败了,也懒得理了config就config吧。

3、写配置文件如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- Register a section handler for the log4net section -->
  <configSections>
    <section name="log4net"  type="log4net.config.log4netconfigurationsectionhandler, log4net" />
  </configSections >
    <log4net>
        <root>
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" />        
        </root>
     
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低)-->
      <logger name="log4nettest">
        <level value="ERROR" />
        <level value="WARN" />
        <level value="INFO" />
      </logger>

      <!--定义输出到文件中-->
      <appender name="LogFileAppender"
               type="log4net.Appender.RollingFileAppender" >
        <!--定义文件存放位置-->
        <param name="File" value="Log/Log" />
        <param name="AppendToFile" value="true" />

        <param name="MaxSizeRollBackups" value="100" />

        <param name="StaticLogFileName" value="false" />

        <param name="DatePattern" value="yyyyMMdd&quot;.Log&quot;" />

        <param name="RollingStyle" value="Date" />

        <layout type="log4net.Layout.PatternLayout">
          <!--每条日志头的文字说明-->
          <!--<param name="Header" value="开始于:"/>
          --><!--每条日志末尾的文字说明--><!--
          <param name="Footer" value="结束"/>-->
          <!--输出格式-->
          <param name="ConversionPattern"
             value="[开始于:%d]%n[[类别: %-5p] %n  %m%n" />
        </layout>

        <!--<filter type="log4net.Filter.LevelRangeFilter">
          <param name="LevelMin" value="DEBUG" />
          <param name="LevelMax" value="WARN" />         
        </filter>-->
      </appender>
      </log4net>

  </configuration>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值