简单跟踪在ASP.NET 4 [英] Simple tracing in ASP.NET 4

查看:103
本文介绍了简单跟踪在ASP.NET 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用System.Diagnostics.Trace类文本文件执行我的应用程序的简单跟踪。我想用这个,而不是log4net的或任何其他解决方案的原因,那是因为......它必须是简单的:)

I'd like to use the System.Diagnostics.Trace class to perform simple tracing of my application in a text file. The reason I want to use this and not log4net or any other solution, it is because ... it has to be simple :)

所以,我在C#中得到了这个工作:

So, I got this working in C#:

        TextWriterTraceListener listener = new TextWriterTraceListener(Server.MapPath("~/App_Data/log.txt"));
        listener.Filter = new EventTypeFilter(SourceLevels.Warning);
        Trace.Listeners.Add(listener);

或等值的Web.config:

Or its equivalent in the Web.config:

  <system.diagnostics>
    <sharedListeners>
      <add name="log" type="System.Diagnostics.TextWriterTraceListener" initializeData="App_Data/log.txt">
        <filter type="System.Diagnostics.EventTypeFilter"  initializeData="Warning" />
      </add>
    </sharedListeners>
    <trace autoflush="true">
      <listeners>
        <clear/>
        <add name="log"/>
      </listeners>
    </trace>
  </system.diagnostics>

所以,当我执行此:

So when I execute this:

        Trace.TraceInformation("info");
        Trace.TraceWarning("warning");
        Trace.TraceError("error");

我得到了有关警告和错误的线路,这就是我所期待。问题是,我怎么使用水平出现在SourceLevels枚举的休息吗?

I got the lines about warning and error, that is what I was expecting. The problem is, how do I use the rest of levels that appears in the SourceLevels enum?

是否有Web应用程序的任何缺陷或使用System.Diagnostics.Trace? (自动冲洗设置为true仅用于测试目的)

Is there any drawback or using System.Diagnostics.Trace for web applications? (autoflush is set to true for testing purposes only)

干杯。

推荐答案

您需要多一点的配置。这code座将记录所有的错误到一个文件,并到另一个所有的信息和ActivityTracing节点。 (这个例子是WCF,您需要将您的名字设置为任何你需要他们。)

You need a little more configuration. This code block will log all errors to one file and all Information and ActivityTracing nodes to another. (This example is for WCF, you will need to set your names to whatever you need them to be.)

<system.diagnostics>
<sources>
  <source name="System.ServiceModel"
          switchValue="Information, ActivityTracing"
          propagateActivity="true" >
    <listeners>
      <add name="xml"/>
    </listeners>
  </source>
  <source name="System.ServiceModel.MessageLogging">
    <listeners>
      <add name="xml"/>
    </listeners>
  </source>
  <source name="ServiceFaultInfoTrace"
          switchName="sourceSwitch"
          switchType="System.Diagnostics.SourceSwitch">
    <listeners>
      <add name="ServiceFaultInfoTraceText" />
      <remove name="Default"/>
    </listeners>
  </source>
</sources>
<switches>
  <add name="sourceSwitch" value="Error"/>
</switches>
<trace autoflush="true" indentsize="4">
  <listeners>
    <add name="ServiceFaultInfoTraceText" />
  </listeners>
</trace>
<sharedListeners>
  <add name="xml"
       type="System.Diagnostics.XmlWriterTraceListener"
       initializeData="C:\log\Api-Traces.svclog" />
  <add name="ServiceFaultInfoTraceText"
       type="System.Diagnostics.TextWriterTraceListener"
       traceOutputOptions="DateTime"
       initializeData="C:\log\Api-Errors.txt" />
</sharedListeners>
</system.diagnostics>

的重要节点是switchValue(和交换机名称)节点。他们设置为您想要记录的来源任何级别。

The important nodes are the switchValue (and switchName) nodes. Set them to whatever level you want logged for the source.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelevel.aspx\">http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelevel.aspx

这篇关于简单跟踪在ASP.NET 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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