3

We have developed our custom HttpModule.
Now I want to add ability to trace it, and see the trace results in standart ASP.NET tracing page (or trace.axd). I try to use System.Diagnostics.Trace.Write("FILTER TEST"); to write trace information. This works everywhere except of HttpModule. I added a trace listener in web.config, but it only shows traces that were written during the page lifecycle. How i can see the trace information that i write in HttpModule and how I can add this information to ASP.NET trace page?

<trace>
  <listeners>
    <add name="WebPageTraceListener"
         type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </listeners>
</trace>
Alex Dn
  • 5,465
  • 7
  • 41
  • 79

2 Answers2

0

Have you tried to use System.Web.IisTraceListener? See: http://www.iis.net/learn/develop/runtime-extensibility/how-to-add-tracing-to-iis-managed-modules

Al3x3
  • 463
  • 3
  • 9
0

I usually use TraceSource and it works fine even within custom HttpModule.

All you need is :

  • Declare a source (you can use as many sources as you want in your code, it depends of what you want for the granularity over your tracing) - (more information on TraceSource here):

    public class MyModule : IHttpModule
    {
        public static readonly TraceSource MyTraceSource = new TraceSource("MySource", SourceLevels.Off);
    (...)
    }
    
  • Within your code, use TraceEvent whenever you need your source to dump something in the trace (more information on the traceEvent method here) :

    MyModule.MyTraceSource.TraceEvent(TraceEventType.Information, 0, "Message that'll be dumped in the trace");
    
  • In your config file you can enable/disable only the tracesource you want at the level you want (information, warning, error, ...)

    <system.diagnostics>
    
        <trace autoflush="true"/>
    
        <sharedListeners>
            <add name="myListener" initializeData="..." type="..."/>
        </sharedListeners>
    
        <sources>
            <source name="MySource" switchName="VerboseSwitch" >
                <listeners>
                    <clear/>
                    <add name="myListener"/>
                </listeners>
            </source>
            <source name="OtherSource" switchName="OffSwitch" >
                <listeners>
                    <clear/>
                    <add name="myListener"/>
                </listeners>
            </source>
        </sources>
    
        <switches>
            <clear/>
            <add name="OffSwitch" value="Off"/>
            <add name="VerboseSwitch" value="Verbose"/>
            <add name="InformationSwitch" value="Information"/>
            <add name="WarningSwitch" value="Warning"/>
            <add name="ErrorSwitch" value="Error"/>
        </switches>
    
    </system.diagnostics>
    

Here you can find a really great article on the trace subject : http://www.codeproject.com/Articles/149251/Debugging-Tracing-and-Instrumentation-in-NET-and-A

Hope this helps !

Sylv21
  • 345
  • 2
  • 11