A Logging the exception information to the database
In the log and monitoring Application block, some friends say that they want to be able to logging the exception information to the database, here to introduce the specific implementation methods.
1. To create an associated database environment:
We can create the relevant database environment using the SQL statements from the log and monitoring application blocks:
To create a database:
CREATE DATABASE [Logging] ON (NAME = N'Logging', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL\data\Logging.mdf' , SIZE = 1, FILEGROWTH = 10%) LOG ON (NAME = N'Logging_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL\data\Logging_log.LDF' , FILEGROWTH = 10%)
To create a table:
CREATE TABLE [dbo].[ Log (
[logid] [int] IDENTITY (1, 1) not NULl,
[EventID] [int] NULl,
[Category] [nvarchar] ($) Collat E SQL _latin1_general _cp1_ci_as not NULL,
[Priority] [int] NOT NULL,
[Severity] [nvarchar] COLLATE SQl _latin1_general _cp1_ci_as NOT NULL,
[Title] [nvarchar] (256) COLLATE SQl _latin1_general _cp1_ci_as not NULL, [Timestamp] [datetime] NOT NULL,
[machinename] [nvarchar] (*) COLLATE SQl _latin1_general _cp1_ci_as NOT NULL ,
[appdomainname] [nvarchar] (2048) COLLATE SQl _latin1_general _cp1_ci_as not NULl,
[ProcessID] [nvarchar] ( 256) COLLATE SQL _latin1_general _cp1_ci_as not NULl,
[processname] [nvarchar] (2048) COLLATE SQL _latin1_general _ Cp1_ci_as not NULl,
[threadname] [nvarchar] (2048) COLLATE SQl _latin1_general _cp1_ci_as NULl,
[Win32thread ID] [nvarchar] (128) COLLATE SQL _latin1_general _cp1_ci_as NULl,
[message] [nvarchar] (2048) COLLATE SQL _latIn1_general _cp1_ci_as null,
[formattedmessage] [ntext] COLLATE SQl _latin1_general _cp1_ci_as null
) on [PRIM ARY] textimage_on [PRIMARY]
Go
Create a stored procedure:
1CREATE PROCEDURE writelog
2 (
3 @EventID int,
4 @Category nvarchar (),
5 @Priority int,
6 @Severity nvarchar,
7 @Title nvarchar (256),
8 @Timestamp datetime,
9 @MachineName nvarchar,
1 0 @AppDomainName nvarchar (2048),
one @ProcessID nvarchar (256),
@ProcessName nvarchar (2048),
@ThreadNa Me nvarchar (2048),
@Win32ThreadId nvarchar (128),
@Message nvarchar (2048),
@FormattedMessage ntext< br>
18AS
INSERT into [Log] (
EventID,
Category,
Priority,
Everity,
Title,
[Timestamp],
MachineName,
Appdomainname,
ProcessID,
ProcessName,
ThreadName,
Win32threadid,
message,
Formattedmessage
35) br> VALUES (
@EventID,
@Category,
@Priority,
@Severity,
@Title,
@Timestamp,
43 @MachineName,
@AppDomainName,
@ProcessID,
@ProcessName,
@ThreadName,
48 @Win32ThreadId,
@Message,
@FormattedMessage)
51GO
The default path for the SQL statement is C:\Program Files\Microsoft Enterprise Library\src\logging\sinks\database\scripts, Run Createloggingdatabase.cmd directly.