Enterprise Library 2.0--Logging Application Block (supplemental)

Source: Internet
Author: User
Tags requires thread

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.