Log4net不同版本在app.config和web.config的配置区别

app.config配置方法(Log4net v1.2.0 )

1,app.config中

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

 <log4net>
     <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<span style="white-space:pre"></span>          <layout type="log4net.Layout.PatternLayout">
<span style="white-space:pre"></span>              <param name="ConversionPattern" value="%d [%t] - %m%n"/>
<span style="white-space:pre"></span>          </layout>
<span style="white-space:pre"></span>     </appender>
     <logger name="debug">
          <appender-ref ref="ConsoleAppender"/>
     </logger>
 </log4net> 

2, AssemblyInfo.cs中添加

[assembly:  log4net.Config.DOMConfigurator(ConfigFileExtension = "config", Watch = true)]
3,定义变量和使用

private static ILog log = LogManager.GetLogger(typeof("类名"))
log.Error("test"); 
 Web.config配置方法(Log4net v1.2.0 )

 1,web.config中(通app.config配置一样)

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

  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="d:\Logs\InfoLog.txt" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="MaximumFileSize" value="1MB" />
      <param name="RollingStyle" value="Size" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p %d [%c] %m%n" />
      </layout>
    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%t]%-5p %c - %m%n"/>
      </layout>
    </appender>

    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingLogFileAppender"/>
      <appender-ref ref="ConsoleAppender"/>
    </root>
  </log4net>
2, AssemblyInfo.cs中添加
[assembly: log4net.Config.DOMConfigurator(new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath("Web.Config ")))]
3,定义变量和使用(通app.config配置一样)
private static ILog log = LogManager.GetLogger(typeof("类名"))
log.Error("test");

可见v1.2.0配置比较麻烦 ,下面是v1.2.10的配置方法

 app.config配置方法(Log4net v1.2.10 )

1,app.config中(还是一样)

2, AssemblyInfo.cs中(可选

[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)] 
3,定义变量和使用
static ILog log = LogManager.GetLogger("somename")
log4net.Config.XmlConfigurator.Configure(); --这个执行一次就可以.
log.Error("test");  
注: 2步中的[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)] 等效于 

  3步中的 log4net.Config.XmlConfigurator.Configure();

Web.config配置方法(Log4net v1.2.10 )

 1,web.config中(一样)

 2, AssemblyInfo.cs中 (这步没了)

3,定义变量和使用

static ILog log = LogManager.GetLogger("somename")
log4net.Config.XmlConfigurator.Configure(); --这个执行一次就可以.
log.Error("test");  

显然v1.2.10引入的 XmlConfigurator增加了配置途径,使webapplication纪日志方便了。

经我测试v1.2.0并不能使用和普通webapplication 相同的配置方式支持wcf. 而v1.2.10新的配置方式使它对wcf依然支持。

 给各完整的config配置 

 

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

    <log4net>
        <!-- Define some output appenders -->
        <appender name="ProjectLogAppender" type="log4net.Appender.RollingFileAppender">
          <param name="File" value="C:\\ServPMSLog\\SvrPlog.txt"/>
          <param name="AppendToFile" value="true"/>
          <param name="MaxSizeRollBackups" value="10"/>
          <param name="MaximumFileSize" value="10000000"/>
          <param name="RollingStyle" value="Size"/>
          <param name="StaticLogFileName" value="true"/>
          <layout type="log4net.Layout.PatternLayout">
            <param name="Header" value="[Header]"/>
            <param name="Footer" value="[Footer]"/>
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
          </layout>
        </appender>

        <appender name="UserLogAppender" type="log4net.Appender.RollingFileAppender">
          <param name="File" value="C:\\ServPMSLog\\SvrUlog.txt"/>
          <param name="AppendToFile" value="true"/>
          <param name="MaxSizeRollBackups" value="10"/>
          <param name="MaximumFileSize" value="10000000"/>
          <param name="RollingStyle" value="Size"/>
          <param name="StaticLogFileName" value="true"/>
          <layout type="log4net.Layout.PatternLayout">
            <param name="Header" value="[Header]"/>
            <param name="Footer" value="[Footer]"/>
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
          </layout>
        </appender>

        <!-- Setup the root category, add the appenders and set the default level -->
        <root>
          <level value="ERROR"/>
          <appender-ref ref="ProjectLogAppender"/>
        </root>

       <logger name="ProjectLog">
          <appender-ref ref="ProjectLogAppender" />
        </logger>

        <logger name="UserLog">
          <appender-ref ref="UserLogAppender" />
        </logger>
    </log4net> 
</configuration> 
阅读更多
换一批

没有更多推荐了,返回首页