Enterprise Library Step by Step Series (12) Exception Handling Application Block

Source: Internet
Author: User
Tags datetime exception handling microsoft sql server mssql create database

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.

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.