Log4net_簡單使用

來源:互聯網
上載者:User

標籤:lin   readline   ogr   滾動   root   ref   前台   hint   elm   

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);        }    }}

  

Log4net_簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.