【问题标题】:Trace logs location, where to view them跟踪日志位置,在哪里查看它们
【发布时间】:2014-10-06 19:32:06
【问题描述】:

在开发 MVC 或 WCF 应用程序时,您在哪里看到 Trace.Write(""); 日志?什么是正确的看点?

【问题讨论】:

    标签: c# wcf trace system.diagnostics


    【解决方案1】:

    使用 System.Diagnostics.Trace 类时,Write 方法写入其跟踪输出“to the trace listeners in the Listeners collection.” Trace.Listeners 属性默认仅包含 DefaultTraceListener 的实例,该实例将消息输出到调试器输出窗口.当然,要查看这些跟踪消息,您必须启用调试。

    因此,如果您在 Visual Studio 中调试 WCF 服务或 ASP.NET 应用程序,您将在 VS 输出窗格中看到跟踪输出。例如这段代码:

    System.Diagnostics.Trace.WriteLine("GetData method was called.");
    

    ...导致出现此输出:

    如果您不想运行调试器来查看跟踪输出,您可以删除 DefaultTraceListener 并将其替换为另一个,例如 TextWriterTraceListener,它将您的跟踪输出到文件。这可以通过创建一个包含以下内容的 web.config 文件来完成(或者只需将 system.diagnostics 部分添加到您预先存在的 web.config 中):

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

    之后(假设您运行在可以写入输出位置的模式),您的跟踪将输出到指定文件。

    如果您想将跟踪记录写入事件日志而不是文件,您也可以使用EventLogTraceListener

    <configuration>
      <system.diagnostics>
        <trace autoflush="false" indentsize="4">
          <listeners>
            <add name="myListener"
              type="System.Diagnostics.EventLogTraceListener"
              initializeData="TraceListenerLog" />
          </listeners>
        </trace>
      </system.diagnostics>
    </configuration>
    

    请注意确保您的应用在有权写入事件日志的帐户上下文下运行。

    您可以对跟踪进行更多操作(例如将其输出到 ASP.NET 页面本身。您将找到包含更多示例的演练here

    【讨论】:

    • 在IIS下运行的时候trace到哪里去了?
    猜你喜欢
    • 1970-01-01
    • 2011-04-01
    • 2014-04-03
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    相关资源
    最近更新 更多