C#使用Log4net输出日志

  1. apache下载地址:https://logging.apache.org/log4net/download_log4net.cgi
  2. 将下载后的文件解压,在bin\net目录下找到程序所需的对应版本
  3. 将该版本中的“log4net.dll”引用到程序中
  4. 并在所需要记录日志的命名空间上添加
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
    也可以不写ConfigFile会自动读取Web.config中log4net的配置
  5. 创建配置文件程序命名为“log4net.config”

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    system.web>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      
      <file type="log4net.Util.PatternString" value="Log/%date{yyyyMMdd}.log" />
      
      <file value="Log/" />
      <datePattern value="yyyyMMdd'.log'"/>
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <maxSizeRollBackups value="15" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        
        <conversionPattern value="[%date]  %thread -- %-5level -- %logger [%M] -- %message%newline" />
      layout>
    appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    root>
  log4net>
configuration>

你可能感兴趣的:(C#,c#,log4net)