Trace question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using Trace in order to log information about the behavior of a web
application. In web.config I have defined my Application Listener as
following:
<trace autoflush="true" indentsize="4">
<listeners>
<add name="AppListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="d:\temp\activity.log" />
<remove type="System.Diagnostics.DefaultTraceListener" />
</listeners>
</trace>

As you can see all activity is logged in "d:\temp\activity.log". Is there a
way to make this variable, that is changing by date automatically, such as:
activity20041201.log,
activity20041202.log,
activity20041203.log etc.

Thank you for your time
 
Not that I know of using the standard .NET FUD. If you want full control,
consider some other form of instrumentation for your application. If you are
really brave, the Enterprise Instrumentation project (MSDN Universal) gives
you real power. For simple logging, however, a variation of the Exception
Management Block (a free download) is an option, as is a custom logger. If
you are stuck on Trace, you have the option of creating your own custom Trace
Listener, which can log wherever you desire.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Spyros said:
I am using Trace in order to log information about the behavior of a
web application. In web.config I have defined my Application
Listener as following:
<trace autoflush="true" indentsize="4">
<listeners>
<add name="AppListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="d:\temp\activity.log" />
<remove type="System.Diagnostics.DefaultTraceListener" />
</listeners>
</trace>

As you can see all activity is logged in "d:\temp\activity.log". Is
there a way to make this variable, that is changing by date
automatically, such as: activity20041201.log,
activity20041202.log,
activity20041203.log etc.

Its easy to create your own trace listener class:

class DateTraceListener : TextWriterTraceListener
{
public DateTraceListener()
{
Name="DateTraceListener";
Initialize();
}
protected void Initialize()
{
string strFile = "Activity"
// + "." + Thread.GetDomainID.ToString() + "."
+ DateTime.Now.ToShortDateString()
+ ".log";
this.Writer = new StreamWriter(strFile, true);
}

}

Use this class instead of TextWriterTraceListener in your config file. (You
need to priovide a fully qualified name, including the assembly name.)

Note the bit I commented out. There's a bug in TextWriterTraceListener that
if you use it from a config file and trace from two appdomains in the
process then the second appdomain will fail because the first appdomain will
have exclusive access to the log file. If you name the file according to the
appdomain name (or in this case the ID) then you avoid this issue. The
ToShortDateString() probably does not return the right format, but its easy
to create the right format with the DataTime properties. Note that you do
not need to use initializeData in your config file.

Finally, to get a new file every day you'll have to close down the instance
of DateTraceListener and create a new one. Unfortunately there's no elegant
way to do this. One solution would be to have a background thread running
and when the date changes it could remove and dispose the DateTraceListener
in the Trace.Listeners collection and then create a new instance and add it
to the collection.

Richard
 
Back
Top