Log4Net使用与配置

===C/S配置===

1.项目中引用log4net.dll

2.在项目的Properties下的AssemblyInfo.cs末尾添加

[assembly: log4net.Config.DOMConfigurator(Watch = true)]

3.在app.config中添加以下节内容

  <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    </configSections>

 <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="SysAppender" />
    </root>
    <logger name="Test">
      <level value="DEBUG" />
      <appender-ref ref="SysAppender" />
    </logger>

  <!-- 配置模板节 由logger节调用name名称 -->
    <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" type="" value="e:\\log" /><!-- 日志文件路径 -->
      <param name="AppendToFile" value="true" />
  <!--<param name="MaxSizeRollBackups" value="12"/>--><!-- 切割最多文件数 -->    
  <!--<param name="MaximumFileSize" value="500KB"/>--><!-- 每个文件的大小 -->
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyyMMdd" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="[%d][%t] %c [%x]%newline%m%newline" /><!-- 日志行配置 -->
      </layout>
    </appender>
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" value="logs\" />
      <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" />        
      </layout>
    </appender>
    <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
  </log4net>

4.添加个调用类

class LogBLL
    {
        public static void debug(string message)
        {
            object o = ConfigurationSettings.GetConfig("log4net");
            log4net.Config.DOMConfigurator.Configure(o as System.Xml.XmlElement);

            log4net.ILog log = log4net.LogManager.GetLogger("WebLogger");
            if (log.IsDebugEnabled)
            {
                log.Debug(message);
            }
            log = null;
        }
        public static void error(string message)
        {
            object o = ConfigurationSettings.GetConfig("log4net");
            log4net.Config.DOMConfigurator.Configure(o as System.Xml.XmlElement);

            log4net.ILog log = log4net.LogManager.GetLogger("WebLogger");
            if (log.IsErrorEnabled)
            {
                log.Error(message);
            }
            log = null;
        }
        public static void fatal(string message)
        {

            object o = ConfigurationSettings.GetConfig("log4net");
            log4net.Config.DOMConfigurator.Configure(o as System.Xml.XmlElement);

            log4net.ILog log = log4net.LogManager.GetLogger("WebLogger");
            if (log.IsFatalEnabled)
            {
                log.Fatal(message);
            }
            log = null;
        }
        public static void info(string message)
        {
            object o = ConfigurationSettings.GetConfig("log4net");
            log4net.Config.DOMConfigurator.Configure(o as System.Xml.XmlElement);

            log4net.ILog log = log4net.LogManager.GetLogger("WebLogger");
            if (log.IsInfoEnabled)
            {
                log.Info(message);
            }
            log = null;
        }

      

        public static void warn(string message)
        {
            object o = ConfigurationSettings.GetConfig("log4net");
            log4net.Config.DOMConfigurator.Configure(o as System.Xml.XmlElement);

            log4net.ILog log = log4net.LogManager.GetLogger("WebLogger");
            if (log.IsWarnEnabled)
            {
                log.Warn(message);
            }
            log = null;
        }
    }

===B/S配置===

1.同样是引用log4net.dll

2.在项目的Properties下的AssemblyInfo.cs末尾添加

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]//这个地方与c/s不同,指定一个特定配置文件

3.新建log4net.config并添加如下内容

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <root>
    <level value="ALL"/>
    <appender-ref ref="SysAppender"/>
  </root>
  <logger name="WebLogger">
    <level value="DEBUG"/>
    <appender-ref ref="SysAppender" />
  </logger>
  <logger name="SocketLogger">
    <level value="DEBUG"/>
    <appender-ref ref="SysAppender" />
  </logger>
  <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
    <param name="File" value="log\" />
    <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>
  </log4net>
4.添加调用类

见C/S

5.在Web.config中configuration/configurations/sectionGroup添加

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> 

 

 

总结:C/S和B/S的配置关键是如何让log4net知道配置文件所在,已经配置文件的正确编写.

你可能感兴趣的:(Log4Net使用与配置)