zoukankan      html  css  js  c++  java
  • Log4net_简单使用

    log4net 有四种主要的组件,分别是Logger(记录器), Repository(库), Appender(附着器)以及 Layout(布局).

    第一步:Log4net的安装

    Install-Package log4net

    第二步:Log4net的配置

    log4net.config

    注意:将log4net.config的属性“复制到输出目录”设置为“始终复制”

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      </configSections>
      
      <log4net>
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
          <!--日志路径-->
          <param name= "File" value= "D:App_Log"/>
          <!--是否是向文件中追加日志-->
          <param name= "AppendToFile" value= "true"/>
          <!--log保留天数-->
          <param name= "MaxSizeRollBackups" value= "10"/>
          <!--日志文件名是否是固定不变的-->
          <param name= "StaticLogFileName" value= "false"/>
          <!--日志文件名格式为:2008-08-31.log-->
          <param name= "DatePattern" value= "yyyy-MM-dd".log""/>
          <!--日志根据日期滚动-->
          <param name= "RollingStyle" value= "Date"/>
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />
          </layout>
        </appender>
        
        <!-- 控制台前台显示日志 -->
        <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
          <mapping>
            <level value="ERROR" />
            <foreColor value="Red, HighIntensity" />
          </mapping>
          <mapping>
            <level value="Info" />
            <foreColor value="Green" />
          </mapping>
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />
          </layout>
    
          <filter type="log4net.Filter.LevelRangeFilter">
            <param name="LevelMin" value="Info" />
            <param name="LevelMax" value="Fatal" />
          </filter>
        </appender>
    
        <root>
          <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
          <level value="all" />
          <appender-ref ref="ColoredConsoleAppender"/>
          <appender-ref ref="RollingLogFileAppender"/>
        </root>
      </log4net>
    </configuration>
    

      

    第三步:调用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    using log4net;
    using log4net.Config;
    
    namespace Log4net
    {
        class Program
        {
            static void Main(string[] args)
            {
                InitLog4Net();
    
                var logger = LogManager.GetLogger(typeof(Program));
    
                logger.Info("消息");
                logger.Warn("警告");
                logger.Error("异常");
                logger.Fatal("错误");
    
                Console.ReadLine();
            }
    
            private static void InitLog4Net()
            {
                var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
                XmlConfigurator.ConfigureAndWatch(logCfg);
            }
        }
    }
    

      解决中文乱码:在 <appender></ appender>中,加上了<param name="Encoding" value="utf-8" />  问题解决!

  • 相关阅读:
    贪心经典例题大赏
    【暑假培训1】7.13
    【7.10校内test】T1高级打字机
    【7.10校内test】T3经营与开发
    【7.10校内test】T2不等数列
    【7.9校内test】T2 极值问题
    【7.9校内test】T1挖地雷
    <知识整理>2019清北学堂提高储备D5
    <知识整理>2019清北学堂提高储备D4
    <知识整理>2019清北学堂提高储备D2
  • 原文地址:https://www.cnblogs.com/ingstyle/p/6655465.html
Copyright © 2011-2022 走看看