Reconfigure an existing RollingFlatFileTraceListener

Topics: Logging Application Block
Feb 11, 2011 at 7:28 AM

Hi,

we use a Logger that is preconfigured via a default.config. This configuration file consists a full configured logging with a RollingFlatfileTraceListener.
The user shall be able to reconfigure parts of this TraceListener via the hosting application configuration (app.config). Other parts shall be hidden, hence we provide a restricted set of configuration possibilities.

Therefor the app.config contains some entries for reconfiguring the TraceListener:

    <appSettings>
        <add key="LogFileName" value="MyOwn.log"/>
        <add key="LogFilePath" value="%LocalAppData%\%UserName%"/>
        <add key="LogFileSeverityFilter" value="Information"/>
    </appSettings>

What is the best practice to reconfigure the existing RollingFlatFileTraceListener with this values?

ConfigurationSourceBuilder starts from scratch (ignoring the default.configuration) as far as I can see.

TIA, Matthias

Feb 11, 2011 at 8:30 AM

I can think of two ways of addressing your requirement:

First is to modify your config at runtime similar to what I posted here.

Or possibly wrap the existing RollingFlatFileTraceListener with a custom trace listener which retrieves the value of its FileName and SeverityFilter from the three (3) appSettings key.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 11, 2011 at 12:39 PM

Hi Sarah,

thank you for this answer. Modifiying the configuration would overwrite the underlying configuration file (my default.config) what is not desired.

A wrapper could be the solution for my problem.

I was reading much of the posts in this forum before I asked my question. I think, many users want to be able to reconfigure the logging items without loosing the old configuration.

It would be a useful thing, if ConfigurationSourceBuilder would have a method to create a configuration based on an existing one.

Regards, Matthias

Feb 14, 2011 at 12:35 AM

I see, I didn't get at first that you are specifically avoiding changing anything in the original config file. 

You can log a feature request for the functionality you want from the ConfigurationSourceBuilder in the Issue Tracker.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 13, 2011 at 1:11 PM

Hi Sarah,
I'm passing through a similar problem as Matthias, and I would like to try the second option that you mentioned:
"…wrap the existing RollingFlatFileTraceListener with a custom trace listener which retrieves the value of its FileName and SeverityFilter from the three (3) appSettings key."
How could I do that? Do you have any example for me, please?
Kind regards.

Apr 14, 2011 at 2:02 AM

I don't have any exact example for that scenario but you can check EntLib 5.0's Extensibility Hands-On Labs for instructions on creating a custom trace listener.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 1, 2011 at 8:49 AM

I solved my problem. The remedy was to write my own implementation of TraceListenerData.

RollingFlatFileTraceListener 

uses

Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData

to get configuration. I wrote my own configuration wrapper:

public class RollingFlatFileTraceListenerData : TraceListenerData
    {
        const string FileNamePropertyName = "fileName";
        ....
       
        /// 
        /// FileName
        /// 
        [ConfigurationProperty(FileNamePropertyName, DefaultValue = "mylogfile.log")]
        public string FileName
        {
            get
            {
                // merge configurations or do any other stuff to manipulate the log file path
                return new ConfigurationMerger().GetLogFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, (string)this[FileNamePropertyName]);
            }
            set
            {
                this[FileNamePropertyName] = value;
            }
        }

       
After that you must provide your own TraceListerData implementation to the RollingFlatFileTraceListener. This can be done via configuration:

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
        <add name="CommonRollingFileListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          listenerDataType="MyOwnNamespace.RollingFlatFileTraceListenerData, MyOwnLibrary"
          fileName="%DEFAULT%\application.log" formatter="Text Formatter"
          ...
         
Hope that helps.

Kind regards, Matthias