在vs2005 C#window项目中使用Log4net

(1)点项目-》添加应用-》浏览,导入log4net.dll
(2)修改app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="clipmapAEC.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    </sectionGroup>
  </configSections>
  <userSettings>
    <clipmapAEC.Properties.Settings>
      <setting name="mapModelMinX" serializeAs="String">
        <value>-80000</value>
      </setting>
      <setting name="mapModelMaxX" serializeAs="String">
        <value>170000</value>
      </setting>
      <setting name="mapModelMinY" serializeAs="String">
        <value>-80000</value>
      </setting>
      <setting name="mapModelMaxY" serializeAs="String">
        <value>170000</value>
      </setting>
      <setting name="scalePara" serializeAs="String">
        <value>2</value>
      </setting>
      <setting name="tileSize" serializeAs="String">
        <value>512</value>
      </setting>
    </clipmapAEC.Properties.Settings>
    <log4net>
      <root>
        <level value="INFO" />
        <appender-ref ref="consoleApp" />
      </root>

      <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
        <param name="File" value="C:\\clipmap_log.txt" />
        <param name="AppendToFile" value="false" />
        <param name="RollingStyle" value="Date" />
        <param name="DatePattern" value="yyyy.MM.dd" />
        <param name="StaticLogFileName" value="true" />
        <layout type="log4net.Layout.PatternLayout,log4net">
          <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
          <param name="Header" value=" ----------------------header--------------------------" />
          <param name="Footer" value=" ----------------------footer--------------------------" />
        </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>
      <logger name="Log4NetTest.LogTest">
        <level value="ERROR" />
        <appender-ref ref="rollingFile" />
        <appender-ref ref="coloredConsoleApp" />
        <appender-ref ref="SystemEvent" />
      </logger>
    </log4net>
  </userSettings>
</configuration>
红色部分是对log4net的设置,其他不用管
(3)在要只用log4net的类namespace前面加using log4net; [assembly: log4net.Config.XmlConfigurator()]
(4)在代码中使用logger.info("XXXX");就可以了

你可能感兴趣的:(在vs2005 C#window项目中使用Log4net)