Search code examples
c#.netconfigurationtrace

How to use TraceSource.TraceEvent in a ASP.NET C# web application


I am required to use TraceSource.TraceEvent in an web application I am developing, after reading this and this msdn links

I am working on a project that is already built, I am making changes to it and updating it.

This is how my web.config is defined.

<system.diagnostics>
<sources>
    <listeners>
    <clear/>
    <add name="xml"/>
  </listeners>
</sources>

<sharedListeners>
  <add name="xml" type="System.Diagnostics.XmlWriterTraceListener"
                           traceOutputOptions="Callstack,LogicalOperationStack,ProcessId"
                           initializeData="C:\logs\Test.svclog"/>
</sharedListeners>

<trace autoflush="true" />

 </system.diagnostics>

The code that I am trying to use

var traceSource = new TraceSource("TraceSourceName");
traceSource.TraceEvent(TraceEventType.Error, 2, "Hello World !");

But no logging is done to Test.svclog file, I have created an empty file Test.svclog under the C:\logs\ folder.

Please help me out on this.


Solution

  • I think you forgot to close your source.

    Just modify your code to this

    var traceSource = new TraceSource("TraceSourceName");
    traceSource.TraceEvent(TraceEventType.Error, 2, "Hello World !");
    traceSource.Close();