Dynamically naming Flat File Trace Listener path

Topics: Exception Handling Application Block, General discussion, Logging Application Block
May 18, 2010 at 11:38 PM

Hello all,

Is there any way to use TARGETDIR or other setup project variables in the path of a FlatFileTraceListener? I would like to set up a log file that is in the same folder as the executable file. I would typically use Assembly.GetExecutingAssembly or Environment.CurrentDirectory or some such, but those are run time options and here I need a design time option. Thanks in advance for any ideas and/or suggestions!

May 19, 2010 at 3:28 AM
Edited May 19, 2010 at 3:58 AM


I haven't personally tried using the TARGETDIR property in the setup project if it is feasible to use it to define to where your log file will be logged.

Though, other options that you may want to consider are

1. Use environment variable.

2. Modify the config file during runtime where you can define where exactly you would want to create your log file before your actual logging happens.

     Sample for this would be         

            Configuration xmlConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            LoggingSettings setting = (LoggingSettings)xmlConfiguration.GetSection(LoggingSettings.SectionName);

            FlatFileTraceListenerData flatFile = setting.TraceListeners.Get("Flat File Trace Listener") as FlatFileTraceListenerData;
            flatFile.FileName = @"C:\Program Files\Avanade\trace.log";

Hope these options may help. 

 Gino Terrado
Global Technology and Solutions
Avanade, Inc.

May 19, 2010 at 5:26 AM

Btw, by default if you only specify "trace.log" as value for the File Name property of your Flat File TraceListener it should be created in the same directory of your deployed executable. HTH.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.

May 19, 2010 at 9:36 AM

Thank you Gino. Both responses are very helpful. It would be helpful if the documentation noted that no specified path defaults as you noted.