1. 程式人生 > >log4net配置

log4net配置

server param glob class enabled enable void ppa bsp

 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配置