Simple tracing in ASP.NET 4

試著忘記壹切 提交于 2019-12-06 10:48:58

问题


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 :)

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);

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");

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?

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

Cheers.


回答1:


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>

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

http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelevel.aspx



来源:https://stackoverflow.com/questions/7016005/simple-tracing-in-asp-net-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!