Yasser Shaikh
Yasser Shaikh

Reputation: 47774

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.

Upvotes: 1

Views: 3755

Answers (3)

Vinod Srivastav
Vinod Srivastav

Reputation: 4255

You may use Nlog for better control over logging as it comes with an inbuild TraceListener Listener too:

If you have defined TraceSource in dll files as :


//dll_01.dll
var traceSource = new TraceSource("TraceSource1");
traceSource.TraceEvent(TraceEventType.Error, 2, "Hello World from dll 01!");

//dll_02.dll
var traceSource = new TraceSource("TraceSource2");
traceSource.TraceEvent(TraceEventType.Error, 2, "Hello World from dll 02!");

//dll_03.dll
var traceSource = new TraceSource("TraceSource3");
traceSource.TraceEvent(TraceEventType.Error, 2, "Hello World from dll 03!");

Then you can add nlog as shared listener and can configure your App.Config as

<system.diagnostics>
    <sources>
      <source name="TraceSource1" switchValue="All">
        <listeners><add name="nlog" /></listeners>
      </source>
      <source name="TraceSource2" switchValue="All">
        <listeners><add name="nlog" /></listeners>
      </source>
      <source name="TraceSource3" switchValue="All">
        <listeners><add name="nlog" /></listeners>
      </source>   
    </sources>
    <sharedListeners>
      <add name="nlog" type="NLog.NLogTraceListener, NLog" />
    </sharedListeners>
    <trace autoflush="true" indentsize="4">
      <listeners>
        <add name="nlog" />
        <remove name="Default" />
      </listeners>
    </trace>
</system.diagnostics>

Rest of the configuration will be done at nLog config files.

Upvotes: 0

vaibhav shah
vaibhav shah

Reputation: 5069

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

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You can adjust your source section with this post on msdn

http://msdn.microsoft.com/en-us/library/ms228984(v=vs.90).aspx

Upvotes: 0

Related Questions