码迷,mamicode.com
首页 > Web开发 > 详细

Log4Net可以根据不同的类容输出到不同的文件夹下面

时间:2015-12-16 21:07:36      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

1 配置文件log4net_dcs.config

<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <appSettings>
  </appSettings>
  <log4net>
    <!--一般信息日志-->
    <appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/InfoLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="InfoLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <!--系统日志-->
    <appender name="SysLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/SysLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="SysLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <!--通讯服务日志-->
    <appender name="CommLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/CommLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="CommLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>

    <!--第三方应用服务日志-->
    <appender name="ThirdLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/ThirdLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="ThirdLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>

    <!--数据持久化日志-->
    <appender name="PersistenceLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/PersistenceLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="PersistenceLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="InfoLog"/>
      <appender-ref ref="SysLog"/>
      <appender-ref ref="CommLog"/>
      <appender-ref ref="ThirdLog"/>
      <appender-ref ref="PersistenceLog"/>
    </root>
  </log4net>
</configuration>

2 在AssemblyInfo.cs文件中添加

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

 

3添加帮助类文件C_LogWriter.cs

   /// <summary>
    /// 日志等级
    /// </summary>
    enum LogLevel
    {
        Error,
        Debug,
        Warning,
        Info
    }
    /// <summary>
    /// 日志类型
    /// </summary>
    enum LogType
    {
        InfoLog,
        SysLog,
        CommLog,
        ThirdLog,
        PersistenceLog
    }
    /// <summary>
    /// 日志记录服务
    /// </summary>
    class C_LogWriter
    {
        /// <summary>
        /// 日志接口
        /// </summary>
        private log4net.ILog m_Log;

        public void Init(LogType _LogType)
        {
            string s = _LogType.ToString();
            m_Log = log4net.LogManager.GetLogger(s);
        }
        /// <summary>
        /// 输出错误级别日志
        /// </summary>
        /// <param name="message">输出的消息</param>
        public void Error(string message)
        {
            //记录日志
            WriteLog(LogLevel.Error, message);
        }

        /// <summary>
        /// 输出警告级别日志
        /// </summary>
        /// <param name="message">输出的消息</param>
        public void Warning(string message)
        {
            //记录日志
            WriteLog(LogLevel.Warning, message);
        }

        /// <summary>
        /// 输出信息级别日志
        /// </summary>
        /// <param name="message">输出的消息</param>
        public void Info(string message)
        {
            //记录日志
            WriteLog(LogLevel.Info, message);
        }

        /// <summary>
        /// 输出调试级别日志
        /// </summary>
        /// <param name="message">输出的消息</param>
        public void Debug(string message)
        {
            //记录日志
            WriteLog(LogLevel.Debug, message);
        } 

        /// <summary>
        /// 记录系统日志
        /// </summary>
        /// <param name="logLevel">日志级别</param>
        /// <param name="message">输出的消息</param>
        private void WriteLog(LogLevel logLevel, string message)
        {
            switch (logLevel)
            {
                case LogLevel.Debug:
                    m_Log.Debug(message);
                    break;
                case LogLevel.Error:
                    m_Log.Error(message);
                    break;
                case LogLevel.Info:
                    m_Log.Info(message);
                    break;
                case LogLevel.Warning:
                    m_Log.Warn(message);
                    break;
            }

        }

    }

4 调用

 C_LogWriter log = new C_LogWriter();
            log.Init(LogType.CommLog);
            log.Warning("前苏联,。");


            log = new C_LogWriter();
            log.Init(LogType.InfoLog);
            log.Warning("前。");


            log = new C_LogWriter();
            log.Init(LogType.PersistenceLog);
            log.Warning("年开。");


            log = new C_LogWriter();
            log.Init(LogType.SysLog);
            log.Warning("前年开。");


            log = new C_LogWriter();
            log.Init(LogType.ThirdLog);
            log.Warning("前苏联军队在1值。");

 

Log4Net可以根据不同的类容输出到不同的文件夹下面

标签:

原文地址:http://www.cnblogs.com/cdaq/p/5052055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!