Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding tracelistener to web.config

I want to use below code with a website. Which config sections I should add to web.config to log the output into a file or windows eventlog ?

using System.Diagnostics;

// Singleton in real code
Class Logger
{
   // In constructor: Trace.AutoFlush = false;

   public void Log(message)
   {
       String formattedLog = formatLog(message);
       Trace.TraceInformation(formattedLog);
       Trace.Flush();
   }
}
like image 464
Xaqron Avatar asked Oct 23 '10 13:10

Xaqron


People also ask

How do I enable WCF tracing on my server?

To activate tracing, you must create a trace listener and set a trace level other than "Off" for the selected trace source in configuration; otherwise, WCF does not generate any traces. If you do not specify a listener, tracing is automatically disabled.

How can I trace a WCF service call?

Configure Tracing config file of the WCF service library and add the configuration for system. diagnostics. In this configuration we are using "Critical, Information, ActivityTracing" as switch value. This will generate traces for all unhandled exceptions, successful execution and communication between two components.

How do I check my server's diagnostic trace logs?

Right-click a server. To view the trace log file, select Open Log Files > Trace File from the menu. To view the messages log file, select Open Log Files > Message Log File from the menu.

How do I turn off tracing in WCF?

If you want to disable the trace source, you should use the logMessagesAtServiceLevel , logMalformedMessages , and logMessagesAtTransportLevel attributes of the messageLogging element instead. You should set all these attributes to false.


1 Answers

You should use system.diagnostics section. Here's example from MSDN for text file:

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="myListener" 
          type="System.Diagnostics.TextWriterTraceListener" 
          initializeData="TextWriterOutput.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

This is for system events log: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlogtracelistener.aspx

like image 173
TarasB Avatar answered Oct 07 '22 18:10

TarasB