I would like my application to write out different trace files named like MachineName_UserName_yyyymmdd_hhmmss.txt where the username is the currently logged in user and the time is the start time of the application. The .Net listener TextWriterTraceListener only seems to support a hard coded file name specified in the config file. Is there a way to do this without writing a custom trace listener.
Assuming I have to write a custom trace listener, I have implemented a tracelistener like this:
Imports System.Diagnostics
Public Class MyCustomTraceListener
Inherits TextWriterTraceListener
Public Sub New()
'Need to do it this way as the Base constructor call has to be the first statement
MyBase.New(String.Format("AppNameTraceFile_{0}_{1}_{2}{3}{4}-{5}{6}{7}.txt", _
Environment.MachineName, _
Environment.UserName, _
DateTime.Now.ToString("yyyy"), _
DateTime.Now.ToString("MM"), _
DateTime.Now.ToString("dd"), _
DateTime.Now.ToString("HH"), _
DateTime.Now.ToString("mm"), _
DateTime.Now.ToString("ss")))
Me.IndentSize = 4
End Sub
End Class
In the config file, I have configured the trace source like this:
<system.diagnostics>
<trace autoflush="true"/>
<sources>
<source name="MyTraceSource"
switchName="mySwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<clear/>
<add name="MyTraceListener"
type="MyNameSpace.MyCustomTraceListener"
traceOutputOptions="ProcessId, DateTime, Callstack" />
</listeners>
</source>
</sources>
<switches>
<add name="mySwitch" value="Warning" />
</switches>
</system.diagnostics>
I am creating the trace source like so:
Dim tsTraceSource As TraceSource = New TraceSource("MyTraceSource")
tsTraceSource.TraceEvent(TraceEventType.Warning, 0, "warning message")
However, at startup, I keep getting an error that the type "MyNameSpace.MycustomTraceListener" could not be found.
Does anyone see what the problem is here ?
Thanks.
Don't you need to inform your type assembly?
<add name="MyTraceListener"
type="MyNameSpace.MyCustomTraceListener, MyAssembly"
traceOutputOptions="ProcessId, DateTime, Callstack" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With