log4net

1添加引用

2写配置文件

一定要修改配置文件属性否则无法输出

 

配置文件内容为

 

<?xml version =1.0encoding =utf-8?>

< 配置>

  < configSections >

    < section name =log4nettype =log4net.Config.Log4NetConfigurationSectionHandler,log4net/>

    < section name =dataConfigurationtype =Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,Microsoft.Practices.EnterpriseLibrary.Data/>

  </ configSections >

  < connectionStrings >

    < add name =LineAutoMySqlproviderName =MySql.Data.MySqlClientconnectionString =DataSource = 127.0.0.1; Database = lineauto; User Id = root; Password = 123; CharSet ='utf8'/>

  </ connectionStrings >

  < dataConfiguration defaultDatabase =LineAutoMySql/>

  < log4net >

    < root >

      < level value =DEBUG/>

      < appender-ref ref =FileAppender/>

      < appender-ref ref =ConsoleAppender/>

    </ root >

    <appendername="FileAppender"type="log4net.Appender.RollingFileAppender">

      <filevalue="Log\\log.txt"/>

      <appendToFilevalue="false"/>

      <rollingStylevalue="Size"/>

      <maxSizeRollBackupsvalue="10"/>

      <maximumFileSizevalue="20MB"/>

      <staticLogFileNamevalue="true"/>

      <layouttype="log4net.Layout.PatternLayout">

        <conversionPatternvalue="%date %-5level%logger %method - %message%newline"/>

        <paramname="Header"value="----------------------header--------------------------&#xD;&#xA;"/>

        <paramname="Footer"value="----------------------footer--------------------------&#xD;&#xA;"/>

 

      </layout>

    </appender>

    <appendername="ConsoleAppender"type="log4net.Appender.ConsoleAppender,log4net">

      <layouttype="log4net.Layout.PatternLayout,log4net">

        <conversionPatternvalue="%date %-5level%logger %method - %message%newline"/>

      </layout>

      <filtertype="log4net.Filter.LevelRangeFilter">

        <levelMinvalue="DEBUG"/>

      </filter>

    </appender>

  </log4net>

</configuration>

 

 

 

 

3  初始化

在此文件中添加如下信息

 

[assembly: log4net.Config.XmlConfigurator(ConfigFile= "Log4Net.config", Watch = true)]

 

 

 

 

4使用时声明一个变量

 

 

 

private static readonly log4net.ILog _logger = LogManager.GetLogger(typeof(MainWindow));

 

5 在需要添加输出的地方添加(记得添加命名空间)

 

 

 


阅读更多

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