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

log4net配置

时间:2017-05-11 18:40:10      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:ted   XML   inf   setting   false   配置   send   csharp   encoding   

 public class Log4
    {

        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
        public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");

        //public static void SetConfig()
        //{
        //    log4net.Config.XmlConfigurator.Configure();
        //}

        //public static void SetConfig(FileInfo configFile)
        //{
        //    log4net.Config.XmlConfigurator.Configure(configFile);
        //}
        /// <summary>
        /// 普通的文件记录日志
        /// </summary>
        /// <param name="info"></param>
        public static void WriteLog(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="info"></param>
        /// <param name="se"></param>
        public static void WriteLog(string info, Exception se)
        {
            if (logerror.IsErrorEnabled)
            {
                logerror.Error(info, se);
            }
        }
    }

  

  方法一:

AssemblyInfo.cs 添加

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

方法二:

Global.asax 添加

protected void Application_Start(object sender, EventArgs e)
{
try
{
 Common.Log4.SetConfig(new FileInfo(Server.MapPath("log4net.config")));

}
catch { }
}

log4net.config 配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <appSettings>
  </appSettings>
  <log4net>
    <!--错误日志类-->
    <logger name="logerror">
      <level value="ALL" />
      <appender-ref ref="ErrorAppender" />
    </logger>
    <!--信息日志类-->
    <logger name="loginfo">
      <level value="ALL" />
      <appender-ref ref="InfoAppender" />
    </logger>
    <!--错误日志附加介质-->
    <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\LogError\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="MaxFileSize" value="10240" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyyMMdd".txt"" />
      <param name="RollingStyle" value="Date" />
      <!--布局-->
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p %d [%c] %m%n"  />
      </layout>
    </appender>
    <!--信息日志附加介质-->
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="Log\\LogInfo\\" />
    <param name="AppendToFile" value="false" />
    <param name="MaxFileSize" value="1" />
    <param name="MaxSizeRollBackups" value="100" />
    <param name="StaticLogFileName" value="false" />
    <param name="DatePattern" value="yyyyMMdd".txt"" />
    <param name="RollingStyle" value="Date" />
    <!--信息日志布局-->
      <layout type="log4net.Layout.PatternLayout">
     <param name="ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>"  />
    </layout>
  </appender>
  </log4net>

</configuration>

  

 

log4net配置

标签:ted   XML   inf   setting   false   配置   send   csharp   encoding   

原文地址:http://www.cnblogs.com/Harvard-L/p/6841965.html

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