custom DB logging using enterprise library 4.1

风格不统一 提交于 2019-12-10 11:48:56

问题


We have to create a historical log of all the changed entities. we have defined our custom tables for this purpose. I have to incorporate this tables in Enterprise library logging block and do logging in these tables. I need to write a SP to insert values to these tables.

Till now,what i have got from google is that i have to create a listener inheriting from CustomTraceListener and give my implementation of WriteMessage.

What i need to know is,how will i plug my tables and SP in Enterprise library logging block.


回答1:


If your stored procedure will work with the following interface:

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
)


Then you can use the Database Trace Listener out of the box. Just point to your stored procedure via configuration.

If not, then you will have to do some coding. You could create a Custom Trace Listener but the easiest way may be to copy the FormattedDatabaseTraceListener and modify. (The core implementation is private so extending doesn't get you all the way there.) They've already done most of the work. The only changes would be to modify the ExecuteWriteLogStoredProcedure to do what you need to call your stored procedure. You would also want to create a TraceListenerData class based on FormattedDatabaseTraceListenerData adding the specific configuration items that you need.

Install the Enterprise Library source code and take a look at the FormattedDatabaseTraceListener class.



来源:https://stackoverflow.com/questions/2642749/custom-db-logging-using-enterprise-library-4-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!