.Net 自定义跟踪文件名

标签 .net vb.net trace

我希望我的应用程序写出不同的跟踪文件,命名为 MachineName_UserName_yyyymmdd_hhmmss.txt,其中用户名是当前登录的用户,时间是应用程序的开始时间。 .Net 监听器 TextWriterTraceListener 似乎只支持在配置文件中指定的硬编码文件名。有没有办法在不编写自定义跟踪监听器的情况下执行此操作。

假设我必须编写一个自定义跟踪监听器,我已经实现了一个像这样的跟踪监听器:

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

在配置文件中,我像这样配置了跟踪源:
  <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>

我正在像这样创建跟踪源:
Dim tsTraceSource As TraceSource = New TraceSource("MyTraceSource")
tsTraceSource.TraceEvent(TraceEventType.Warning, 0, "warning message")

但是,在启动时,我不断收到一个错误,提示找不到类型“MyNameSpace.MycustomTraceListener”。

有没有人看到这里有什么问题?

谢谢。

最佳答案

你不需要通知你的类型程序集吗?

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

关于.Net 自定义跟踪文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1603470/

相关文章:

.NET CryptoStream 在 Dispose() 中读取密文的结尾并爆炸

.net - 无法让我的正则表达式匹配

c# - 如何获取 JSON 对象 - C#

.net - 在 VB.NET 中将变量声明为 Byte 时出现的问题

javascript - 如何在 vb.net 2005 中创建一个 json 对象

.net - RSA 和 OAEP : Choosing or not choosing the Hash Algorithm?

rust - 在跟踪中访问 Span 的字段值

c# - 相当于 Windows Metro C# 中的 Trace.Writeline

debugging - 如何在Erlang中使用trace和dbg来调试和跟踪我的程序?

c# - 切换不同的app.config