ENTLIB 5.0 : Logging Application Block with Database Listener

Topics: Building and extending application blocks, Logging Application Block
Jul 10, 2012 at 6:22 AM

Hi there,

I am using Enterprise Library 5.0 and My task is to Log information to Database. For this I used Enterprise Library Logging Application Block with Database Trace Listener.

Now, after few days I found that I need some more information to Log into the Logging.LOG Table. They are : User_ID, Session_ID etc.

Per Business requirements, I can't use ExtendedProperty Property of LogEntry class and store this information in the FormattedMessage column of the Log Table in XML fomrat. I want proper Column with Name "User_ID" and "Session_ID" in the Log Table of Logging Database.

Question Is: How can I push customized information in the Log Table?

For this I found something called "CustomTraceListener" where we need to override two methods i.e. Write(string Message) & WriteLog(string Message)

* How Can I use this CustomeTraceListener to push custome message to Log Table?

* Write method takes single parameter of type string, means will I get a concatinated string which I need to break into different information and push into database using my own code?

Can someone please send me an implimentation example of CustomeTraceListener to --> Database


Please help me.







Jul 10, 2012 at 8:28 AM

Why this blog is so silent?

Jul 10, 2012 at 1:31 PM

I'm pretty new to this stuff and was just exploring the same question yesterday.  I'm not sure if it would work, but a pretty simple workaround *might* be to create an indexed view based on the original table, where the view parses out your additional columns based on the text formatting rules that you use when you create the FormattedMessage field.  Then you log as you are now without needing to change anything, and you do your reporting on the indexed view.  Like I said, not sure if it will work but I thought I would pose it as an idea.

If that didn't work, a real table that you insert to from an insert trigger to the Log table might be another option without getting into custom EntLib extensions.

Jul 11, 2012 at 4:46 AM

There is a sample project, Custom Database Trace Listener Sample on the Samples page that shows how you could do something similar to what you want.  It uses a CustomLogEntry class to add the additional data and then the custom trace listener extracts that data and logs it to a CustomData column that was added to the Log table.  Feel free to modify and add additional columns/properties to suit your needs.  Note that the LogWriter.Write method has an overload that accepts a LogEntry which contains all of the information available to be logged; this class can be extended to create a CustomLogEntry.

You can also take a look at the Enterprise Library 5.0 - Extensibility Labs for hands on labs in extending Enterprise Library in various scenarios.

Randy Levy
Enterprise Library support engineer