I would like my application to write different trace files with the name MachineName_UserName_yyyymmdd_hhmmss.txt, where the username is currently the registered user and the time is the start time of the application. Reading the .Net Listener TextWriterTraceListener seems to support the hard-coded file name specified in the configuration file. Is there a way to do this without writing a custom trace listener.
Assuming I need to write my own trace listener, I applied tracelistener as follows:
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 configuration file, I configured the trace source as follows:
<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 create a trace source like this:
Dim tsTraceSource As TraceSource = New TraceSource("MyTraceSource")
tsTraceSource.TraceEvent(TraceEventType.Warning, 0, "warning message")
, MyNameSpace.MycustomTraceListener .
- , ?
.