Microsoft Enterprise Library 5.0 series (4) logging Application Block

Source: Internet
Author: User

Working principle of the enterprise database log application module:


We can see the working principle of the Enterprise Library log application module. The logfilter, trace source, trace listener, and log formatter information can all be reflected in the category configuration file, by calling the writer method of the logwriter class in the configuration file, you can write the logentry entity containing the log information to the specified device in the category configuration file.

The Enterprise Library diary application module provides the following recording methods::

  • The Event Log
  • An e-mail message
  • A database
  • A Message Queue
  • A text file
  • A windowsmanagement instrumentation (Wmi) event
  • Custom locationsusing Application Block Extension points

This article describes how to use the Enterprise Library diary application module to Send Logs to log files.,Database, XMLWrite Program operation logs to files:


I.Write a log to the log file

  1. Run entlibconfig.exe and selectBlocksMenu, clickAdd loggingsettings.

2. Configure each module:

3. Click the File menu, and click Save to save it as an app. config file. You can save it to the desktop and use it later.

4. Create a new console applicationApp. configAdd to the program and add the required DLL file. What we want to import here isMicrosoft. Practices. enterpriselibrary. Logging. dll, Microsoft. Practices. enterpriselibrary. Logging. database. dllAnd add the reference:

Add reference:

using Microsoft.Practices.EnterpriseLibrary. Logging;

5. test:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Using Microsoft. Practices. enterpriselibrary. logging;

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Logentry = new logentry ();

Logentry. eventid = 1;
Logentry. Priority = 1;
Logentry. Title = "title party ";
Logentry. Message = "http://www.cnblogs.com/huangcong ";
Logentry. categories. Add ("C # Learning ");
Logentry. categories. Add ("Microsoft Enterprise Library learning ");

Logger. Writer. Write (logentry, "general ");
Console. writeline ("log writing completed! ");
}
}
}

Running result:

6. Start-control panel-system and security-view Event Logs-Windows logs-applications:

7. The first log is the log we just written. You can double-click it to view the content:

2. write a diary to the database

1. to write logs to the database, you must first create a new log database. You can find the loggingdatabase in the source \ blocks \ Logging \ SRC \ databasetracelistener \ scripts directory under the entlib50src folder. the SQL file can automatically generate a log database as long as it is run in the database:

For the convenience of everyone, I directly publish this file below, and you can copy it directly to SQL to run it:

Create an SQL statement for a logging Database

/****** Object: Database Logging Script Date: 8/22/2005 ******/

USE [master]

GO



IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'Logging')

DROP DATABASE [Logging]

GO



CREATE DATABASE [Logging]

COLLATE SQL_Latin1_General_CP1_CI_AS

GO



exec sp_dboption N'Logging', N'autoclose', N'false'

GO



exec sp_dboption N'Logging', N'bulkcopy', N'false'

GO



exec sp_dboption N'Logging', N'trunc. log', N'false'

GO



exec sp_dboption N'Logging', N'torn page detection', N'true'

GO



exec sp_dboption N'Logging', N'read only', N'false'

GO



exec sp_dboption N'Logging', N'dbo use', N'false'

GO



exec sp_dboption N'Logging', N'single', N'false'

GO



exec sp_dboption N'Logging', N'autoshrink', N'false'

GO



exec sp_dboption N'Logging', N'ANSI null default', N'false'

GO



exec sp_dboption N'Logging', N'recursive triggers', N'false'

GO



exec sp_dboption N'Logging', N'ANSI nulls', N'false'

GO



exec sp_dboption N'Logging', N'concat null yields null', N'false'

GO



exec sp_dboption N'Logging', N'cursor close on commit', N'false'

GO



exec sp_dboption N'Logging', N'default to local cursor', N'false'

GO



exec sp_dboption N'Logging', N'quoted identifier', N'false'

GO



exec sp_dboption N'Logging', N'ANSI warnings', N'false'

GO



exec sp_dboption N'Logging', N'auto create statistics', N'true'

GO



exec sp_dboption N'Logging', N'auto update statistics', N'true'

GO



use [Logging]

GO



SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[Category]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)

BEGIN

CREATE TABLE [dbo].[Category](

[CategoryID] [int] IDENTITY(1,1) NOT NULL,

[CategoryName] [nvarchar](64) NOT NULL,

CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED

(

[CategoryID] ASC

) ON [PRIMARY]

) ON [PRIMARY]

END

GO

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[CategoryLog]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)

BEGIN

CREATE TABLE [dbo].[CategoryLog](

[CategoryLogID] [int] IDENTITY(1,1) NOT NULL,

[CategoryID] [int] NOT NULL,

[LogID] [int] NOT NULL,

CONSTRAINT [PK_CategoryLog] PRIMARY KEY CLUSTERED

(

[CategoryLogID] ASC

) ON [PRIMARY]

) ON [PRIMARY]

END

GO

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[Log]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)

BEGIN

CREATE TABLE [dbo].[Log](

[LogID] [int] IDENTITY(1,1) NOT NULL,

[EventID] [int] NULL,

[Priority] [int] NOT NULL,

[Severity] [nvarchar](32) NOT NULL,

[Title] [nvarchar](256) NOT NULL,

1544172660 [datetime] NOT NULL,

[MachineName] [nvarchar](32) NOT NULL,

[AppDomainName] [nvarchar](512) NOT NULL,

[ProcessID] [nvarchar](256) NOT NULL,

[ProcessName] [nvarchar](512) NOT NULL,

[ThreadName] [nvarchar](512) NULL,

[Win32ThreadId] [nvarchar](128) NULL,

[Message] [nvarchar](1500) NULL,

[FormattedMessage] [ntext] NULL,

CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED

(

[LogID] ASC

) ON [PRIMARY]

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

END

GO

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[InsertCategoryLog]') AND type in (N'P', N'PC'))

BEGIN

EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE InsertCategoryLog

@CategoryID INT,

@LogID INT

AS

BEGIN

SET NOCOUNT ON;



DECLARE @CatLogID INT

SELECT @CatLogID FROM CategoryLog WHERE CategoryID=@CategoryID and LogID = @LogID

IF @CatLogID IS NULL

BEGIN

INSERT INTO CategoryLog (CategoryID, LogID) VALUES(@CategoryID, @LogID)

RETURN @@IDENTITY

END

ELSE RETURN @CatLogID

END

'

END

GO

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[AddCategory]') AND type in (N'P', N'PC'))

BEGIN

EXEC dbo.sp_executesql @statement = N'



CREATE PROCEDURE [dbo].[AddCategory]

-- Add the parameters for the function here

@CategoryName nvarchar(64),

@LogID int

AS

BEGIN

SET NOCOUNT ON;

DECLARE @CatID INT

SELECT @CatID = CategoryID FROM Category WHERE CategoryName = @CategoryName

IF @CatID IS NULL

BEGIN

INSERT INTO Category (CategoryName) VALUES(@CategoryName)

SELECT @CatID = @@IDENTITY

END



EXEC InsertCategoryLog @CatID, @LogID



RETURN @CatID

END



'

END

GO

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[ClearLogs]') AND type in (N'P', N'PC'))

BEGIN

EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE ClearLogs

AS

BEGIN

SET NOCOUNT ON;



DELETE FROM CategoryLog

DELETE FROM [Log]

DELETE FROM Category

END

'

END

GO

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[WriteLog]') AND type in (N'P', N'PC'))

BEGIN

EXEC dbo.sp_executesql @statement = N'







/****** Object: Stored Procedure dbo.WriteLog Script Date: 10/1/2004 3:16:36 PM ******/



CREATE PROCEDURE [dbo].[WriteLog]

(

@EventID int,

@Priority int,

@Severity nvarchar(32),

@Title nvarchar(256),

@Timestamp datetime,

@MachineName nvarchar(32),

@AppDomainName nvarchar(512),

@ProcessID nvarchar(256),

@ProcessName nvarchar(512),

@ThreadName nvarchar(512),

@Win32ThreadId nvarchar(128),

@Message nvarchar(1500),

@FormattedMessage ntext,

@LogId int OUTPUT

)

AS



INSERT INTO [Log] (

EventID,

Priority,

Severity,

Title,

1544172660,

MachineName,

AppDomainName,

ProcessID,

ProcessName,

ThreadName,

Win32ThreadId,

Message,

FormattedMessage

)

VALUES (

@EventID,

@Priority,

@Severity,

@Title,

@Timestamp,

@MachineName,

@AppDomainName,

@ProcessID,

@ProcessName,

@ThreadName,

@Win32ThreadId,

@Message,

@FormattedMessage)



SET @LogID = @@IDENTITY

RETURN @LogID







'

END

GO

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'FK_CategoryLog_Category') AND parent_obj = OBJECT_ID(N'[dbo].[CategoryLog]'))

ALTER TABLE [dbo].[CategoryLog] WITH CHECK ADD CONSTRAINT [FK_CategoryLog_Category] FOREIGN KEY( [CategoryID])

REFERENCES [dbo].[Category] ( [CategoryID])

GO

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'FK_CategoryLog_Log') AND parent_obj = OBJECT_ID(N'[dbo].[CategoryLog]'))

ALTER TABLE [dbo].[CategoryLog] WITH CHECK ADD CONSTRAINT [FK_CategoryLog_Log] FOREIGN KEY( [LogID])

REFERENCES [dbo].[Log] ( [LogID])

GO



SET QUOTED_IDENTIFIER ON

SET ARITHABORT ON

SET CONCAT_NULL_YIELDS_NULL ON

SET ANSI_NULLS ON

SET ANSI_PADDING ON

SET ANSI_WARNINGS ON

SET NUMERIC_ROUNDABORT OFF

go



DECLARE @bErrors as bit



BEGIN TRANSACTION

SET @bErrors = 0



CREATE NONCLUSTERED INDEX [ixCategoryLog] ON [dbo].[CategoryLog] ([LogID] ASC, [CategoryID] ASC )

IF( @@error <> 0 ) SET @bErrors = 1



IF( @bErrors = 0 )

COMMIT TRANSACTION

ELSE

ROLLBACK TRANSACTION

2. ClickLogging target listenersThe plus sign button in the upper right corner of the block,Add logging target listenersThen clickAdd database listeners, The property settings are as follows:

3. File-save: Save app. config, and directly run the program in vs without modifying it. The running result is as follows:

4. view the logging database and view the log you just written:

SELECT * FROM [Logging].[dbo].[Category]
GO
SELECT * FROM [Logging].[dbo].[Log]
GO

3. write a diary to the XML file

1. Create an XML file namedTest. xml, ClickLogging target listenersThe plus sign button in the upper right corner of the block,Add logging target listenersThen clickAdd XML listeners, The property settings are as follows:

2. File-save: Save app. config and directly run the program in vs without modifying it. The running result is as follows:

3. Open the test. xml file with the following content:

Haha. Well, today's tutorial is here for lunch ~ Sleep ~ Go to class in the afternoon (the lab courses in the school are the most annoying )~

Enter

Source: http://www.cnblogs.com/huangcong/archive/2010/05/31/1748672.html please enter

The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.