custom DB logging using enterprise library 4.1

2019-06-09 13:35发布

问题:

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.