企业库日志应用程序模块工作原理图:
从上图我们可以看清楚企业库日志应用程序模块的工作原理,其中LogFilter,Trace Source,Trace Listener,Log Formatter的信息都可以在Category配置文件中反映出来,通过配置文件,调用LogWriter类的Writer方法,就可以将包含日志信息的LogEntry实体写入Category配置文件中指定的设备了.
企业库日记应用程序模块提供了下列几种记录方法:
- The event log
- An e-mail message
- A database
- A message queue
- A text file
- A Windows?Management Instrumentation (WMI) event
- Custom locationsusing application block extension points
本文介绍一下如何使用企业库日记应用程序模块向日志文件,数据库,XML文件中写入程序操作日记:
一. 向日志文件写入日记
- 运行EntLibConfig.exe,选择Blocks菜单 ,单击 Add LoggingSettings .
2. 对每个模块进行配置:
3. 点击File菜单,单击Save,保存为一个App.config文件,可以先保存到桌面,之后要用到它.
4. 创建一个新的控制台应用程序,将App.config添加到程序内,并加入需要的Dll文件,在此我们要导入的是 Microsoft.Practices.EnterpriseLibrary. Logging.dll, Microsoft.Practices.EnterpriseLibrary. Logging.Database.dll并添加需要的引用:
添加引用:
5. 测试:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace ConsoleApplication1
{
class Program
{
staticvoid Main(string[] args)
{
LogEntry logEntry =new LogEntry();
logEntry.EventId =1;
logEntry.Priority =1;
logEntry.Title ="标题党";
logEntry.Message ="http://www.cnblogs.com/huangcong/";
logEntry.Categories.Add("C#学习");
logEntry.Categories.Add("Microsoft Enterprise Library学习");
Logger.Writer.Write(logEntry, "General");
Console.WriteLine("日志写入完成!");
}
}
}
运行结果:
6. 开始—控制面板—系统和安全—查看事件日志—Windows日志—应用程序:
7. 第一个日志就是我们刚刚写入的日志啦,双击可以查看内容:
二. 向数据库写入日记
1. 要将日志写入数据库,就得先新建一个日志数据库,我们可以在EntLib50Src文件夹下的Source\Blocks\Logging\Src\DatabaseTraceListener\Scripts目录中找到LoggingDatabase.sql文件,只要在数据库中运行即可自动生成一个日志数据库:
为了方便大家,我直接将该文件发布在下面,大家直接复制到SQL中运行也可以:
USE[master]
GO
IFEXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N‘Logging‘)
DROPDATABASE[Logging]
GO
CREATEDATABASE[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
IFNOTEXISTS (SELECT*FROM dbo.sysobjects WHERE id =OBJECT_ID(N‘[dbo].[Category]‘) ANDOBJECTPROPERTY(id, N‘IsUserTable‘) =1)
BEGIN
CREATETABLE[dbo].[Category](
[CategoryID][int]IDENTITY(1,1) NOTNULL,
[CategoryName][nvarchar](64) NOTNULL,
CONSTRAINT[PK_Categories]PRIMARYKEYCLUSTERED
(
[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
IFNOTEXISTS (SELECT*FROM dbo.sysobjects WHERE id =OBJECT_ID(N‘[dbo].[CategoryLog]‘) ANDOBJECTPROPERTY(id, N‘IsUserTable‘) =1)
BEGIN
CREATETABLE[dbo].[CategoryLog](
[CategoryLogID][int]IDENTITY(1,1) NOTNULL,
[CategoryID][int]NOTNULL,
[LogID][int]NOTNULL,
CONSTRAINT[PK_CategoryLog]PRIMARYKEYCLUSTERED
(
[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
IFNOTEXISTS (SELECT*FROM dbo.sysobjects WHERE id =OBJECT_ID(N‘[dbo].[Log]‘) ANDOBJECTPROPERTY(id, N‘IsUserTable‘) =1)
BEGIN
CREATETABLE[dbo].[Log](
[LogID][int]IDENTITY(1,1) NOTNULL,
[EventID][int]NULL,
[Priority][int]NOTNULL,
[Severity][nvarchar](32) NOTNULL,
[Title][nvarchar](256) NOTNULL,
[Timestamp][datetime]NOTNULL,
[MachineName][nvarchar](32) NOTNULL,
[AppDomainName][nvarchar](512) NOTNULL,
[ProcessID][nvarchar](256) NOTNULL,
[ProcessName][nvarchar](512) NOTNULL,
[ThreadName][nvarchar](512) NULL,
[Win32ThreadId][nvarchar](128) NULL,
[Message][nvarchar](1500) NULL,
[FormattedMessage][ntext]NULL,
CONSTRAINT[PK_Log]PRIMARYKEYCLUSTERED
(
[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
IFNOTEXISTS (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
IFNOTEXISTS (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
IFNOTEXISTS (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
IFNOTEXISTS (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,
[Timestamp],
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
IFNOTEXISTS (SELECT*FROM sysobjects WHERE id =OBJECT_ID(N‘FK_CategoryLog_Category‘) AND parent_obj =OBJECT_ID(N‘[dbo].[CategoryLog]‘))
ALTERTABLE[dbo].[CategoryLog]WITHCHECKADDCONSTRAINT[FK_CategoryLog_Category]FOREIGNKEY( [CategoryID])
REFERENCES[dbo].[Category] ( [CategoryID])
GO
IFNOTEXISTS (SELECT*FROM sysobjects WHERE id =OBJECT_ID(N‘FK_CategoryLog_Log‘) AND parent_obj =OBJECT_ID(N‘[dbo].[CategoryLog]‘))
ALTERTABLE[dbo].[CategoryLog]WITHCHECKADDCONSTRAINT[FK_CategoryLog_Log]FOREIGNKEY( [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@bErrorsasbit
BEGINTRANSACTION
SET@bErrors=0
CREATENONCLUSTEREDINDEX[ixCategoryLog]ON[dbo].[CategoryLog] ([LogID]ASC, [CategoryID]ASC )
IF( @@error<>0 ) SET@bErrors=1
IF( @bErrors=0 )
COMMITTRANSACTION
ELSE
ROLLBACKTRANSACTION
2. 点击Logging Target Listeners 区块右上角的加号按钮, Add Logging Target Listeners然后点击 Add Database Listeners ,属性设置如下:
3. File—Save,保存App.config,无需改动VS中的程序直接再次运行,运行结果如下:
4. 查看Logging数据库,可以看到刚刚写入的日志:
GO
SELECT*FROM[Logging].[dbo].[Log]
GO
三. 向XML文件写入日记
1. 创建一个XML文件,我建在上面创建好的工程目录下,命名为test.xml,点击Logging Target Listeners 区块右上角的加号按钮, Add Logging Target Listeners然后点击 Add XML Listeners ,属性设置如下:
2. File—Save,保存App.config,无需改动VS中的程序直接再次运行,运行结果如下:
3. 打开test.xml文件,内容如图所示:
哈哈.好了,今天的教程就做到这里,吃午餐~睡觉~下午上课去(最讨厌学校的实验课了)~
黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block,布布扣,bubuko.com
黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block