Access trace listener property created from app.config

Topics: Logging Application Block
Jan 24, 2007 at 10:27 AM
I would simplify my problem to this small one:
I would like to change trace listener “header” property in my code. I would like to make Logging application block configuration from app.config (not programmatically).

What is clear to me is that I will have to make at least one Logger.Write call before accessing the property. After first Loger.Write call trace listeners are already created in memory.
However Logger.Writer.structureHolder is defined as a private so I am not able to access TraceSources in the holder.

I do not want to modify EL souce code because of future EL updates.

How can I change header property?

Jan 24, 2007 at 6:19 PM
Typically the format of your log messages is handled by a Formatter Object, such as the Text Formatter, Binary Formatter, or a Custom Formatter. You typically set the formatter template at design time via the configuration tool.

In some cases, a trace listener or formatter could expose a specific header property to specify an actual header for the messages, but that is not expected or even common and will vary for each tracelistener or formatter. You would have to check if it is available and writable at runtime for the particular tracelistener / formatter you are using.

Without doing a lot of work, you could use the ExtendedProperties Collection of the Log Entry Class to pull off a custom header for each message. It is a bit kludgy, but adding the token - {keyvalue()} to the top of your formatter template may do the trick and then you can just add a single ExtendedProperty to each LogEntry instance at runtime that would be your header.

For more information on programmatically manipulating the Logging Application Block at runtime, you can read a tutorial I wrote that may help you in your endeavors:




David Hayden
Microsoft MVP C#
Jan 25, 2007 at 2:07 PM
Hi David,
Thank you for your reply.
As I said in my original message Header property was just a sample to quick explain what I need.
In the fact I have developed my own listener with a public property called FileHeader. FileHeader is written to new created file whenever new file is created, I can setup FileHeader in a config file. But what I would like to realy to do is to set FileHeader in the runtime (in one of our application e.g. they what to write timestamp of starting of their application and version of application; The reason for FileHeader is that if they use just simple message then within several days the original message will be deleted.)
ExtendedProperties I tried already before for a stack trace. It works fine, but this is not exactly what I would like to do. In this case I need to access my trace listener public property FileHeader (my trace listener is configured in app.config, not programmatically; if programmatically created then it will be easy, but in this case I would loose configurability).
Best regards,