跟踪日志的位置以及在何处查看它们 [英] Trace logs location, where to view them

查看:76
本文介绍了跟踪日志的位置以及在何处查看它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

在使用System.Diagnostics.Trace类时,Write方法将其跟踪输出写入 会将您的跟踪输出到文件。这可以通过创建具有以下内容的web.config文件来完成(或只需将system.diagnostics部分添加到您先前存在的web.config中):

 < configuration> 
< system.diagnostics>
< trace autoflush = true indentsize = 4>
< listeners>
<删除名称=默认 />
<添加名称= myListener type = System.Diagnostics.TextWriterTraceListener
initializeData = c:\myListener.log />
< / listeners>
< / trace>
< /system.diagnostics>
< / configuration>

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



如果您想将跟踪写到事件日志而不是文件中,也可以使用< a href = http://msdn.microsoft.com/zh-cn/library/system.diagnostics.eventlogtracelistener(v=vs.110).aspx rel = noreferrer> EventLogTraceListener :

 < configuration> 
< system.diagnostics>
< trace autoflush = false indentsize = 4>
< listeners>
<添加名称= myListener
type = System.Diagnostics.EventLogTraceListener
initializeData = TraceListenerLog />
< / listeners>
< / trace>
< /system.diagnostics>
< / configuration>

请务必确保您的应用在帐户环境下运行并有权写入事件日志。



跟踪还可以做很多事情(例如将其输出到ASP.NET页面本身。您将找到更多示例的演练此处


Where do you see Trace.Write(""); logs while developing an MVC or WCF app? What is the correct place to look at?

解决方案

When using the System.Diagnostics.Trace class, the Write method writes its trace output "to the trace listeners in the Listeners collection." The Trace.Listeners property by default only contains an instance of the DefaultTraceListener, which outputs messages to the debugger output window. To view those trace messages, you have to enable debugging, of course.

So if you debug your WCF service or ASP.NET app in Visual Studio, you will see the trace output in the VS Output pane. For example, this code:

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

...causes this output to appear:

If you don't want to run a debugger to see the trace output, you can remove the DefaultTraceListener and replace it with another, e.g., a TextWriterTraceListener that will output your trace to a file. This can be done by creating a web.config file with the following content (or just add the system.diagnostics section to your pre-existing 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>

After that (assuming you are running in a mode that has access to write to the output location), your traces will be output to the specified file.

If you want to write your traces to the Event Log instead of to a file, you can do that too with an EventLogTraceListener:

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

Just take care to ensure that your app is running under an account context with access to write to the Event Log.

There is a lot more you can do with tracing (such as have it output to the ASP.NET page itself. You'll find a walkthrough with more examples here.

这篇关于跟踪日志的位置以及在何处查看它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆