views:

18

answers:

1

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();
   }
}
+3  A: 

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

Taras B
Logging to windows event-log is not straight forward with asp.net since it requires administrative privileges.
Xaqron
do you think any changes in the web.config _listiners_ section is required to allow logging into EventLog ?
Taras B