Logging Application Block With SQL Server 2005 (2)

Logging Application Block With SQL Server 2005 (2)

開啟 SQL Server Management Syudio 並登入

  • 建立資料表 aspnet_entlog


(

id int IDENTITY NOT NULL, EventId int NULL, Priority int NULL, Category varchar( 1000 ) NULL, Title varchar( 500 ) NULL, Message varchar( 1000 ) NULL, Machine varchar( 100 ) NULL, Timestamps smalldatetime NULL, Severity varchar( 100 ) NULL, ApplicationDomain varchar( 1000 ) NULL, ProcessId int NULL, ProcessName varchar( 500 ) NULL, Win32ThreadId int NULL, ThreadName varchar( 500 ) NULL, ExtendedProperties varchar( 1000 ) NULL, PRIMARY KEY (id) )

  • 建立預存程序 AddCategory


 (

@categoryName varchar( 1000 ), @logID int

 )

AS begin UPDATE aspnet_entlog SET Category=@categoryName WHERE ID=@logID end
  • 建立預存程序 WriteLog
CREATE procedure WriteLog

 (

@EventId int, @Priority int, @Title varchar( 500 ), @Message varchar( 4000 ), @machineName varchar( 100 ), @Timestamp smalldatetime, @Severity varchar( 100 ), @AppDomainName varchar( 1000 ), @ProcessId int, @ProcessName varchar( 500 ), @Win32ThreadId int, @ThreadName varchar( 500 ), @formattedmessage varchar( 4000 ), @LogId int out )

AS

begin INSERT INTO aspnet_entlog (

EventId, Priority, Title, Message, Machine, Timestamps, Severity, ApplicationDomain, ProcessId, ProcessName, Win32ThreadId, ThreadName, ExtendedProperties )

VALUES

(

@EventId, @Priority, @Title, @Message, @machineName, @Timestamp, @Severity, @AppDomainName, @ProcessId, @ProcessName, @Win32ThreadId, @ThreadName, @formattedmessage )

SET @LogId=@@identity end
  • 程式:加入參考選擇 .NET 組件,加入 Enterprise Library Logging Application BlockEnterprise Library Logging Application Block Database Provider
  • 引入命名空間
    using Microsoft.Practices.EnterpriseLibrary.Logging;
  1. LogEntry log = new LogEntry( );
  2. log.EventId = 404;
  3. log.Title = "系統登入";
  4. log.Message = "使用者: " + Login1.UserName + " 登入失敗 ";
  5. log.Categories.Add ( "General" );
  6. log.Severity = TraceEventType.Warning;
  7. log.Priority = 9;
  8. log.TimeStamp = DateTime.Now;
  9. log.AppDomainName = "註冊服務系統";
  10. Logger.Write (log);