Log4Net是apache推出來的,它支援多種程式語言的log,如官方網站所示:
PHP、.NET、java、C++..
步驟一:
AssemblyInfo.cs載入組件設定
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
步驟二:
新增資料表至MS SQL
CREATE TABLE [dbo].[Log] (
[Id] [int] IDENTITY (1, 1) NOT NULL,
[Date] [datetime] NOT NULL,
[Thread] [varchar] (255) NOT NULL,
[Level] [varchar] (50) NOT NULL,
[Logger] [varchar] (255) NOT NULL,
[Message] [varchar] (4000) NOT NULL,
[Exception] [varchar] (2000) NULL
)
步驟三:設定log4n.config
configSections
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=[資料庫連線];initial catalog=[資料庫名稱];integrated security=false;persist security info=True;User ID=[使用者];Password=[密碼]" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="All" />
←分為七級,可自行設定OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL
←這邊設定所有等級的log都要寫入資料庫做記錄
<appender-ref ref="AdoNetAppender" />
</root>
步驟四:Test.aspx
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WebForm1));
protected void Page_Load(object sender, EventArgs e){log.Info("^_^");
}
步驟五:看資料庫有沒有成功寫入資料
步驟六:成功^_^
2..NET寺庙
沒有留言:
張貼留言