.NET跟踪到一个文件无法正常工作无法正常、文件、工作、NET

2023-09-04 02:42:29 作者:住进时光里

我想跟踪奇怪的事情会在我的Windows窗体应用程序与TextWriterTraceListener会指着一个文件位置。我把它设置,以便在第一时间应用程序需要的程序运行期间跟踪的东西,它会创建跟踪侦听器并将其注册。

I am trying to track strange things going on in my Windows Forms application with a TextWriterTraceListener pointed to a file location. I have it set up so that the first time the application needs to trace something during the run of the program, it creates the trace listener and registers it.

不过,它看起来像跟踪文件不获取所有创建的,没有出现在C:\ GMS2Trace.log。我已经验证,该方案已经达到了code调用跟踪方法的部分。

However, it looks like the trace file is not getting created at all, nothing showed up at C:\GMS2Trace.log. I have verified that the program has reached parts of the code that call the trace method.

我的一丝code是这样的:

My trace code looks like:

internal static void traceWarning(string message)
{
    if (!traceEnabled)
    {
        traceEnabled = true;
        Trace.Listeners.Add(new TextWriterTraceListener(@"C:\GMS2Trace.log"));
    }

    Trace.TraceWarning(getTimeStamp() + " " + message);
}

时将其与跟踪文件的位置,或者其他什么东西出了问题?

Is it a problem with the location of the trace file, or something else?

推荐答案

您可以将其所有的的app.config配置,只需使用:

You can configure it all from app.config and just use:

Trace.Writeline("msg");

这是我的一个项目举例:

Example from one of my projects:

<system.diagnostics>
  <trace autoflush="true" indentsize="4">
    <listeners>
      <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="log.log" />
      <remove name="Default" />
    </listeners>
  </trace>
</system.diagnostics>

但请记住,所有Console.Writeline allso结束了在文件

Remember though that all Console.Writeline allso ends up in the file