Log4Net簡單使用

來源:互聯網
上載者:User

標籤:errorlog   eve   log   使用   param   stat   ado   set   sse   

     1.設定檔添加

<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net>
<!--<logger name="logerror">
<level value="ERROR" />
<appender-ref ref="ErrorAppender" />
</logger>-->
<logger name="loginfo">
<level value="INFO" />
<appender-ref ref="InfoAppender" />
</logger>
<!--<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\\LogError\\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n異常時間:%d %n異常層級:%-5p%n異常內容:%m%n" />
</layout>
--><!--&lt; &gt; = <> %n = 斷行符號--><!--
</appender>-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="LogInfo\\" />
<param name="AppendToFile" value="true" />
<param name="MaxFileSize" value="10240" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="日誌時間:%d %n記錄層級:%-5p %n日誌內容:%m%n%n" />
</layout>
</appender>
</log4net>

2.添加Log4Net引用檔案

3.配置[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]在 AssemblyInfo 下

4.調用方法寫日誌

 1 public class LogHelper 2 { 3 public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo"); 4  5 public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); 6  7 //public static void SetConfig() 8 //{ 9 // log4net.Config.XmlConfigurator.Configure();10 //}11 12 //public static void SetConfig(FileInfo configFile)13 //{14 // log4net.Config.XmlConfigurator.Configure(configFile);15 //}16 17 public static void WriteLog(string info)18 {19 if (loginfo.IsInfoEnabled)20 {21 loginfo.Info(info);22 }23 }24 25 public static void WriteErrorLog(string info)26 {27 if (logerror.IsErrorEnabled)28 {29 logerror.Error(info);30 }31 }32 }

 

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.