Change backing file for FlatFileTraceListener at runtime?

Topics: Logging Application Block
Jun 27, 2008 at 1:13 PM
Hello,

I have been using the Logging application block from Enterprise Library 3.1 in a .Net 2 console application.

I expect several instances of the console app to be run at the same time, and each is given a log file name as a command line argument.

How can I use this parameter to set the file that my FlatFileTraceListener writes to?

Otherwise, how can I ensure that each instance of the console application writes to a different (named) log file?

Thanks for any clues and pointers to the appropriate docs ('cos I can't find them by myself!)

Steven
Jun 27, 2008 at 2:11 PM
Edited Jun 27, 2008 at 2:12 PM
Hi,

You can use environment variables to achieve this:

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            string loggingName = (args.Length > 0) ? args[0] : "default";

 

            Environment.SetEnvironmentVariable("test", loggingName, EnvironmentVariableTarget.Process);

 

            Logger.Write("test");

        }

    }

}


      <add

        fileName="%test%-rolling.log"

        footer="----------------------------------------"

        header="----------------------------------------"

        rollFileExistsBehavior="Overwrite"

        rollInterval="None"

        rollSizeKB="0"

        timeStampPattern="yyyy-MM-dd"

        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        traceOutputOptions="None"

        filter="All"

        type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        name="Rolling Flat File Trace Listener" />


Hope this helps,
Fernando

sjmack wrote:
Hello,

I have been using the Logging application block from Enterprise Library 3.1 in a .Net 2 console application.

I expect several instances of the console app to be run at the same time, and each is given a log file name as a command line argument.

How can I use this parameter to set the file that my FlatFileTraceListener writes to?

Otherwise, how can I ensure that each instance of the console application writes to a different (named) log file?

Thanks for any clues and pointers to the appropriate docs ('cos I can't find them by myself!)

Steven



Jun 27, 2008 at 3:25 PM
That looks nice, but doesn't work for me (Enterprise Library 3.1). I just get a log file called, for example, "%LogFileName%". I think the environment variables are mentioned in the change log for new features in Enterprise Library 4.0; I can't use that because my project targets .Net 2.

Is it possible to programatically extend the configuration section that defines my logging?
Jun 27, 2008 at 4:09 PM

I'm sorry, didn't notice the version you're using.

What kind of extension are you thinking of?

Fernando

Jun 27, 2008 at 4:30 PM
Maybe I don't mean an "extension"?

I just want to change the log file name. So can I modify an existing TraceListener in code, or can I instantiate a new TraceListener in code and add it to an existing category?

Or is it easier to write my own custom FlatFileTraceListener that would pick up the environment variable value, or has a static method that would let me set the log file name?

Or just compile the Logging from ELib 4 for .Net 2 ... ?


Jun 30, 2008 at 4:56 PM

Hi,

I think creating a new trace listener that inherits from FlatFileTraceListener to implement the environment variable replacement is the most convenient approach, even if you'll need to provide design-time support.

You cannot modify an existing trace listener (the file name is only used to initialize a stream), and adding new trace listeners through code could be done but it's not recommended. And you cannot re-target EntLib 4.0 to .NET 2.0; it wouldn't compile.

Fernando


sjmack wrote:
Maybe I don't mean an "extension"?

I just want to change the log file name. So can I modify an existing TraceListener in code, or can I instantiate a new TraceListener in code and add it to an existing category?

Or is it easier to write my own custom FlatFileTraceListener that would pick up the environment variable value, or has a static method that would let me set the log file name?

Or just compile the Logging from ELib 4 for .Net 2 ... ?





Jul 1, 2008 at 12:04 PM
Hi, thanks for your continuing help with this!

I've made a class derived from FlatFileTraceListener. It holds a static field with a public accessor "FileName", and if FileName has a value then it will use that in preference to any value passed in to the constructor which I assume is read in from the config file.

My next problem is how to reference my new class in the configuration file, preferably using the configuration application. You said "you'll need to provide design-time support". Can you point me at an example or documentation for this?
Jul 1, 2008 at 3:18 PM
Hi,

You will find some information in this thread http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=30114.

Fernando


sjmack wrote:
Hi, thanks for your continuing help with this!

I've made a class derived from FlatFileTraceListener. It holds a static field with a public accessor "FileName", and if FileName has a value then it will use that in preference to any value passed in to the constructor which I assume is read in from the config file.

My next problem is how to reference my new class in the configuration file, preferably using the configuration application. You said "you'll need to provide design-time support". Can you point me at an example or documentation for this?