.Net Custom Trace File Name

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 .

- , ?

.

+5
4

?

<add name="MyTraceListener"
     type="MyNameSpace.MyCustomTraceListener, MyAssembly"
     traceOutputOptions="ProcessId, DateTime, Callstack" />
+1

, :

TextWriterTraceListener config xml, Listeners, ( DefaultTraceListener TextWriterTraceListener), .

0

TraceListener ? , . log path, app.config, appSettings .

' VB
Dim traceLog As New FileStream(traceFile, FileMode.OpenOrCreate)
Dim traceListener As New TextWriterTraceListener(traceLog)
Trace.Listeners.Add(traceListener)

// C#
FileStream traceLog = new FileStream(traceFile, FileMode.OpenOrCreate);
TextWriterTraceListener traceListener = new TextWriterTraceListener(traceLog);
Trace.Listeners.Add(traceListener);

API, , .

traceListener.TraceOutputOptions |= TraceOptions.Callstack | TraceOptions.DateTime | TraceOptions.ProcessId;
0

, - - , , : http://msdn.microsoft.com/en-us/library/ff664768%28v=pandp.50%29.aspx

Application Log Block http://msdn.microsoft.com/en-us/library/ff664569%28v=pandp.50%29.aspx

, nuget Visual Studio -

In the event that you want to delve deeply into the enterprise magazine registration application block, here is the entire PDF book: http://www.microsoft.com/en-us/download/details.aspx?id=41145 p>

0
source