开发者

Simple tracing in ASP.NET 4

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.TraceInformat开发者_StackOverflow中文版ion("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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜