RollingFlatFileTraceListener with System.Diagnostics.Trace

Topics: Logging Application Block
Dec 7, 2010 at 2:49 PM


Is it possible to use the Rolling Flat File Trace listener with the System.Diagnostics something like the code snippet below, I tried but it did not work, I am not using the configuration setup at all. I would really appreciate if you would be able to help me out here.

 string sLogFile = "C:\\Inetpub\\wwwroot\\TestApp\\TestApp\\";

            sLogFile += System.DateTime.Now.ToString("s").Replace(":", "-");

            sLogFile += @".Log";


Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener obj = new RollingFlatFileTraceListener(sLogFile, "", "", null, 0, "mm-dd-yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Minute);


System.Diagnostics.Trace.AutoFlush = true;

System.Diagnostics.Trace.WriteLine(“Writing to log file”);

Dec 8, 2010 at 2:51 AM


I really haven't tried this one until just now. I have just copy and pasted your code and was able to log (though in a different location). What exactly the error you have encountered with your code? Could you try logging in a directory you have a write permission (C:/temp).  Anyway, one better approach I know of if you prefer not using any configuration file and you are using Entlib version 5 is utilizing Fluent Configuration API. Please see a reference from the documentation for more details regarding this - Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.

Dec 8, 2010 at 3:01 PM

Hi Gino,

Thanks a lot for your reply.

I dont get any errors while executing the code, the code works well and it does write logs to a flat file. The only problem I am facing is that it does not rollover the flat file based on my criteria of either time or size. Please let me know if this can be acheived by the code snippet I provided earlier, we are trying to upgrade our logging using entlib and if we are able to do it using the System.Diagnostics.Trace, we will have to make least code changes.


Dec 9, 2010 at 12:55 AM
Edited Dec 9, 2010 at 1:04 AM

It's because the logic of rolling the file is in the RollingFlatFileTraceListener's TraceData method but the code above calls the WriteLine method.  Since there's no exposed TraceData method in the Trace object, what you can do is create a sublass of the RollingFlatFileTraceListener, provide the necessary constructor and override the WriteLine method so that it calls base.TraceData method.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Dec 9, 2010 at 8:47 PM

Hi Sarah,

Thanks for your reply, I did implement the solution that you suggested and it worked for the fact that the files did get rolled over but the results were unpredictable. Please review the code snippet below and let me know if anything is incorrect.

string sLogFile =



obj = new MyClass(sLogFile, "", "", null, 10, "mm-dd-yyyy", RollFileExistsBehavior.Overwrite, RollInterval.None);    




Trace.AutoFlush = true;



msg = "Writing Application_Start message";


obj .WriteLine(

string.Format("Main Thread Exception 1:{0}", msg));

 //My Class







class MyClass : RollingFlatFileTraceListener



public MyClass(string sLogFile, string str1, string str2, string str3, int data, string str4, RollFileExistsBehavior bhvr, RollInterval

intvl): base(sLogFile, str1, str2, null, data, str4, bhvr ,intvl)



public override void WriteLine(string o){


base.TraceData(null, "FI Web Service", System.Diagnostics.TraceEventType.Critical,2,o);




Dec 14, 2010 at 1:10 AM

Can you describe the unpredictable cases you're encountering?


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Dec 14, 2010 at 4:27 PM


Please try the code that i pasted in my earlier post, the files did not rollover as expected based on the criteria of the file size.


Dec 15, 2010 at 6:59 AM

I've tried the code pasted and it was able to roll over at around 10Kb (approximately 10 to 11 Kb). I'm wonering what's happening from your end?

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.


Dec 15, 2010 at 8:41 PM

Thanks, we are now following a different approach. I have a new query mentioned below.

In the package log4net it is possible to control the level of logging at a class level

and it is possible to check at runtime if the current level of logging in the class (or category) has been enabled.

E.g. log4net has properties like IsDebugEnabled, IsInfoEnabled, and IsWarnEnabled which can be used to

suppress extraneous string creation and other preparatory work done for logging.


In EntLib we can use the IsLoggingEnabled() to check if the logging has been enabled. This check is performed

at the application level. Does the Entlib provide flags which can be configured to control the logging at the class level?

And can we setup a configuration for the different categories of logging, as in the case of log4net’s IsDebugEnabled, etc.



static LogWriter defaultWriter;

defaultWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();


if (defaultWriter.IsLoggingEnabled())


<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging"

        enabled="true" name="LogEnabled Filter" />

Dec 16, 2010 at 5:39 AM

"Does the Entlib provide flags which can be configured to control the logging at the class level?"

- You mean check entlib configuration and apply changes which is only in effect inside the class?  You can apply changes to logging configuration using the Fluent Configuration API.   Note though in the sample code, the last line which is:

EnterpriseLibraryContainer.Current  = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

applies whatever change you made in the code to the logging configuration.  If you need to revert back to the original connfiguration after the class has finished execution, you can create a FileConfigurationSource out of your config file and replace the EnterpriseLibraryContainer.Current object again with that config source.

EnterpriseLibraryContainer.Current  = EnterpriseLibraryContainer.CreateDefaultContainer(new FileConfigurationSource(appplicationConfigurationFileName));

"And can we setup a configuration for the different categories of logging, as in the case of of log4net's IsDebugEnabled, etc?"

- I'm not sure I get this, I haven't used log4net but I researched what exactly is IsDebugEnabled and saw that it is only a flag to check if the logger is is enabled for Debug events.  Can you explain your question further? 

By the way, please create new discussion thread for different questions.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.