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

Log4web独立config配置

时间:2016-06-30 17:53:22      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

第一步:config配置,独立文件的

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>

  <log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <root>
      <level value="ALL"/>
      <!--<appender-ref ref="SysAppender"/>-->
    </root>

    <!-- Print only messages of level DEBUG or above in the packages -->
    <logger name="SysAppenderLogger">
      <level value="ALL"/>
      <appender-ref ref="SysAppender"/>
    </logger>
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" value="Logger/ttt/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
        <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
        <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
      </layout>
    </appender>

    <logger name="WeiXinResultLogger">
      <level value="ALL"/>
      <appender-ref ref="WeiXinResult"/>
    </logger>
    <appender name="WeiXinResult" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" value="Logger/weixinresult/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
        <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
        <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
      </layout>
    </appender>

  </log4net>
</configuration>

第二步:log4Helper配置

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"weixin\Log4.config", Watch = true)]
namespace SERP3.Common
{
    /// <summary>
    ///错误记录日志 
    ///注意:在需要记录log的类前加入[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"weixin\Log4.config", Watch = true)]
    /// </summary>
    public class Log4Helper
    {

        //private static readonly log4net.ILog log = log4net.LogManager.GetLogger("SysAppender123Logger");
        //private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//动态获得名称

        public Log4Helper()
        {

        }
         
        private static log4net.ILog log(string appenderName)
        {
            return log4net.LogManager.GetLogger(appenderName);
        }

        public static void LogInfo(string Message, string appenderName = "defaultLogger")
        {
            log4net.ILog logger = log(appenderName);
            if (logger.IsInfoEnabled) logger.Info(Message);
        }

        public static void LogInfo(string appenderName, string Message, Exception ex)
        {
            log4net.ILog logger = log(appenderName);
            if (logger.IsInfoEnabled)
                logger.Info(Message, ex);
        }
        public static void ErrorInfo(string appenderName, string Message)
        {
            log4net.ILog logger = log(appenderName);
            if (logger.IsInfoEnabled) logger.Error(Message);
        }

        public static void ErrorInfo(string appenderName, string Message, Exception ex)
        {
            log4net.ILog logger = log(appenderName);
            if (logger.IsInfoEnabled)
                logger.Error(Message, ex);
        }
        //public static void DebugInfo(string appenderName, string Message)
        //{
        //    if (!log(appenderName).IsInfoEnabled)
        //        SetConfig();
        //    log(appenderName).Debug(Message);
        //}

    }
}

第三步:调用

try
        {
            Result result = CreateResult(xmlStr);
            res = result.Save();
        }
        catch (Exception ex)
        {
            SERP3.Common.Log4Helper.ErrorInfo("WeiXinResultLogger",ex.Message+xmlStr,ex);
        }

注意点:1.[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"weixin\Log4.config", Watch = true)]

namespace SERP3.Common
{

Log4web独立config配置

标签:

原文地址:http://www.cnblogs.com/zhuyapeng/p/5630509.html

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