zoukankan      html  css  js  c++  java
  • Log4net使用方法

    一、Web项目中加入log4net:

    1、nuget中加入log4net

    2、webconfig中的<configuration></configuration>中加入以下节点:

    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
      </configSections>
      <log4net>
        <root>
          <level value="INFO" />
          <appender-ref ref="FileAppender" />
          <appender-ref ref="ConsoleAppender" />
        </root>
        <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
          <file value="logs\" />
          <appendToFile value="true" />
          <rollingStyle value="Composite" />
          <datePattern value="yyyy-MM-dd'.txt'" />
          <maxSizeRollBackups value="200" />
          <maximumFileSize value="10485760" />
          <staticLogFileName value="false" />
          <layout type="log4net.Layout.PatternLayout">
            <header value="[Header] " />
            <footer value="[Footer] " />
            <conversionPattern value="%date %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
      </log4net>

    3、在Global.asax.cs中加入:

     log4net.Config.XmlConfigurator.Configure();

    二、控制台中加入log4net

        1、app.config中加入节点:

      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
      </configSections>
      <log4net>
        <root>
          <level value="INFO" />
          <appender-ref ref="FileAppender" />
          <appender-ref ref="ConsoleAppender" />
        </root>
        <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
          <file value="logs\" />
          <appendToFile value="true" />
          <rollingStyle value="Composite" />
          <datePattern value="yyyy-MM-dd'.txt'" />
          <maxSizeRollBackups value="200" />
          <maximumFileSize value="10485760" />
          <staticLogFileName value="false" />
          <layout type="log4net.Layout.PatternLayout">
            <header value="[Header] " />
            <footer value="[Footer] " />
            <conversionPattern value="%date %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
      </log4net>

    2、AssemblyInfo.cs 中最后加入这句:

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

    3、实际使用:

       log4net.ILog logInfo = log4net.LogManager.GetLogger("loginfo");
                logInfo.Info("测试日志写入");
                Console.WriteLine("日志写入文件完成");
                Console.ReadLine();
  • 相关阅读:
    html5和css3的新特性
    实现全选按钮的js代码
    window.location对象获取浏览器地址栏的地址信息
    珍爱网前端笔试题之九宫格的实现
    c# array arraylist list
    解决visual studio不能发现单元测试、无法运行单元测试的方法
    Linux 学习笔记
    C++语言学习
    C语言学习
    日志打印,设置开关类【编程技巧】
  • 原文地址:https://www.cnblogs.com/25miao/p/7028232.html
Copyright © 2011-2022 走看看