資源簡介
在config文件中的配置
要使用log4net,首先要在config文件的<configSections>節點中增加配置(如果沒有這個節點請手動增加),如下:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
除此之外,還要在頂級節點<configuration>下增加<log4net>子節點。在<log4net>節點下就可以增加<appender>子節點,每個<appender>子節點代表一種記錄日志的方式(僅在這里配置了不代表啟用了)。
具體說來有如下Appender:
AdoNetAppender:利用ADO.NET記錄到數據庫的日志。
AnsiColorTerminalAppender:在ANSI 窗口終端寫下高亮度的日志事件。
AspNetTraceAppender:能用asp.net中Trace的方式查看記錄的日志。
BufferingForwardingAppender:在輸出到子Appenders之前先緩存日志事件。
ConsoleAppender:將日志輸出到控制臺。
EventLogAppender:將日志寫到Windows Event Log.
FileAppender:將日志寫到文件中。
LocalSyslogAppender:將日志寫到local syslog service (僅用于UNIX環境下).
MemoryAppender:將日志存到內存緩沖區。
NetSendAppender:將日志輸出到Windows Messenger service.這些日志信息將在用戶終端的對話框中顯示。
RemoteSyslogAppender:通過UDP網絡協議將日志寫到Remote syslog service。
RemotingAppender:通過.NET Remoting將日志寫到遠程接收端。
RollingFileAppender:將日志以回滾文件的形式寫到文件中。
SmtpAppender:將日志寫到郵件中。
TraceAppender:將日志寫到.NET trace 系統。
UdpAppender:將日志connectionless UDP datagrams的形式送到遠程宿主或以UdpClient的形式廣播。
關于使用log4net中可能會使用到的一些參數
%m(message):輸出的日志消息,如ILog.Debug(…)輸出的一條消息
%n(new line):換行
%d(datetime):輸出當前語句運行的時刻
%r(run time):輸出程序從運行到執行到當前語句時消耗的毫秒數
%t(thread id):當前語句所在的線程ID
%p(priority): 日志的當前優先級別,即DEBUG、INFO、WARN…等
%c(class):當前日志對象的名稱,例如:
%f(file):輸出語句所在的文件名。
%l(line):輸出語句所在的行號。
%數字:表示該項的最小長度,如果不夠,則用空格填充,如“%-5level”表示level的最小寬度是5個字符,如果實際長度不夠5個字符則以空格填充。
下面以一個實際的例子來說明問題,比如在配置中有“%date [%thread] (%file:%line) %-5level %logger [%property{NDC}] - %message%newline”,那么實際的日志中會是如下格式:
“記錄時間:2010-11-17 16:16:36,561 線程ID:[9] 日志級別:文件:所在行ERROR 出錯類:Log4NetDemo.Program property:[(null)] - 錯誤描述:error
System.Exception: 在這里發生了一個異常,Error Number:2036084948”
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Management;
namespace?Log4netExpand
{
????public?partial?class?Default?:?System.Web.UI.Page
????{
????????protected?void?Page_Load(object?sender?EventArgs?e)
????????{
????????}
????????protected?void?Button1_Click(object?sender?EventArgs?e)
????????{
????????????//log4net.ILog?log?=?log4net.LogManager.GetLogger(“myLogger“);
????????????LogContent?logmodel?=?new?LogContent();
????????????logmodel.Event_Type?=?3;
????????????logmodel.EventCategory?=?“登陸系統“;
????????????logmodel.Event_ID?=?1;
????????????logmodel.ComputerName?=?Request.UserHostAddress;
????????????logmodel.Mac_Address?=?GetMacAddress();
????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-05-03?22:57??log4net\
?????目錄???????????0??2015-05-03?22:16??log4net\Lib\
?????文件??????286720??2012-01-05?11:25??log4net\Lib\log4net.dll
?????目錄???????????0??2015-05-03?22:16??log4net\Log4netExpand\
?????文件?????????672??2012-01-05?11:25??log4net\Log4netExpand\Default.aspx
?????文件????????1955??2012-01-05?11:25??log4net\Log4netExpand\Default.aspx.cs
?????文件????????1483??2012-01-05?11:25??log4net\Log4netExpand\Default.aspx.designer.cs
?????文件????????4709??2012-01-05?11:25??log4net\Log4netExpand\Log4netExpand.csproj
?????文件????????1086??2012-01-05?11:25??log4net\Log4netExpand\Log4netExpand.csproj.user
?????目錄???????????0??2015-05-03?22:16??log4net\Log4netExpand\Properties\
?????文件????????1345??2012-01-05?11:25??log4net\Log4netExpand\Properties\AssemblyInfo.cs
?????文件????????1240??2012-01-05?11:25??log4net\Log4netExpand\Web.Debug.config
?????文件????????1301??2012-01-05?11:25??log4net\Log4netExpand\Web.Release.config
?????文件????????5633??2012-01-05?11:25??log4net\Log4netExpand\Web.config
?????目錄???????????0??2015-05-03?22:16??log4net\Log4netExpand\bin\
?????文件????????9216??2015-05-03?22:15??log4net\Log4netExpand\bin\Log4netExpand.dll
?????文件???????26112??2015-05-03?22:15??log4net\Log4netExpand\bin\Log4netExpand.pdb
?????文件??????286720??2012-01-05?11:25??log4net\Log4netExpand\bin\log4net.dll
?????目錄???????????0??2015-05-03?22:16??log4net\Log4netExpand\code\
?????文件????????1438??2012-01-05?11:25??log4net\Log4netExpand\code\LogContent.cs
?????文件????????1890??2012-01-05?11:25??log4net\Log4netExpand\code\LogHelper.cs
?????文件?????????412??2012-01-05?11:25??log4net\Log4netExpand\code\MyLayout.cs
?????文件????????1958??2012-01-05?11:25??log4net\Log4netExpand\code\MyMessagePatternConverter.cs
?????目錄???????????0??2015-05-03?22:16??log4net\Log4netExpand\obj\
?????目錄???????????0??2015-05-03?22:16??log4net\Log4netExpand\obj\Debug\
?????文件????????7048??2015-05-27?16:13??log4net\Log4netExpand\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????2045??2015-05-03?22:15??log4net\Log4netExpand\obj\Debug\Log4netExpand.csproj.FileListAbsolute.txt
?????文件????????9903??2015-05-03?22:15??log4net\Log4netExpand\obj\Debug\Log4netExpand.csprojResolveAssemblyReference.cache
?????文件????????9216??2015-05-03?22:15??log4net\Log4netExpand\obj\Debug\Log4netExpand.dll
?????文件???????26112??2015-05-03?22:15??log4net\Log4netExpand\obj\Debug\Log4netExpand.pdb
?????文件???????10107??2012-01-05?11:25??log4net\Log4netExpand\obj\Debug\ResolveAssemblyReference.cache
............此處省略37個文件信息
- 上一篇:C# 實時讀取EXCEL到SQL數據庫
- 下一篇:ABP源碼注釋中文版
評論
共有 條評論