In the previous article, a friend mentioned that we often write logs of events (such as exceptions) to the database in the actual project. This is more convenient to view and manage, and in the previous article I have written to the text file as an example, today this article is a supplement to the first two articles, Mainly to describe how to write log information to the database, but also thank the friend's suggestion, below we go to the point, divided into the following steps:
First, according to Enterprise Library 2.0 to write the log to the database needs, we add a log in the database table (Rx_log):--Log Information table
CREATE TABLE Rx_log
(
ID int identity NOT NULL,--serial number
EventId int NULL,--Event ID
Priority int null,-priority
Category varchar (1000) NULL, --Category
Title varchar ($) NULL,--log title
Message varchar (1000) NULL,--Log information
Machine varchar (MB) NULL,-hostname
timestamps smalldatetime null,--record time
SEv erity varchar (MB) NULL,-severity
Applicationdomain varchar (1000) NULL, application name
Proces sId int NULL,--Process ID
processname varchar ($) NULL,--process name
Win32threadid int NULL,--thread ID
threadname varchar (+) NULL,--thread name
extendedproperties var CHAR (1000) NULL,--extended information
Primary key (ID)
)
Logging Application Block in the log to the database process, also requires two stored procedures (Note: This is also a small difference with 1.0, the previous version requires only one stored procedure), respectively:
--Establish a stored procedure
--Write a log
CREATE PROCEDURE Usp_writelogtodatabase
(
@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 Rx_log
(
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