Custom Logging

Topics: Logging Application Block
Apr 4, 2007 at 11:59 AM
Edited Apr 4, 2007 at 12:08 PM
Hello everybody.

I'm a bit lose in the Logging Application Block. I did the HOL exercises, and I added a simple logging (to system event log) and tracer (to a flat file log) to my application, and for there I have no problem.

But know, requisites told me that I have to log some data to a Database table which is already defined and these data aren't in the standard LogEntry.

If someone could told me wich steps I should follow or some reference documentation, I'll be very thankful. I don't know how to custom my log entry, and how to insert it into database.

Thank's for your attention.
Apr 4, 2007 at 5:16 PM
Hi Sergio -

The key steps are:

  1. Modify or subclass the LogEntry class to hold whatever data you need to store
  2. Ideally, create some stored procedures that will be used to access the log database
  3. Either modify the existing DatabaseTraceListener or create your own based on the existing code, which will pull apart your customized LogEntry and call the relevant stored procedures.

Hope this helps
Apr 5, 2007 at 7:02 PM

There is an example of a custom log entry in the LEVMAH project.
The class is ErrorLogEvent.
Using a combination of some custom tokens and a modified TextFormatter you could spit out the custom properties to any Trace listener you want including the DatabaseTraceListener(with the LEVMAHXmlFormatter as your formatter).

Apr 10, 2007 at 7:11 AM
Thanks very much for both.

I'm now back from my easter holidays :( and i'll go for it.

Thanks again.
Apr 12, 2007 at 10:24 AM
Hi again!

I'm trying to understand this, and I only want to know if this steps are ok.

First i have to create my own MyCustomLogEntry which inherits from LogEntry class.

Second I have to write or modify my own TextFormatter class based on the MyCustomLogEntry class.

I hope it's correct.

Also I have another question. When instead using Logging, I use tracing. I have to configure app.config for using my custom TextFormatter class for write myCustomLogEntry in the log. haven't i?

Thanks again and sorry by the inconvenience. This applicattion block is getting me crazy (a bit more ;))

Apr 12, 2007 at 6:19 PM
Hi Sergio -

Step 1 is correct.
Step 2 is not necessary if you are using EntLib 3.0 or if you applied the Reflected Property Token change I described in my blog last year.

Tracing and Logging go through the same underlying mechanisms - the Tracer class will automatically log events at the start and end of the code block. However the Tracer is hard-wired to use a regular LogEntry class, so if you want it to use your own custom LogEntry-derived class you'll need to modify the Tracer code or build your own Tracer equivalent.