locked
Logging to Event Log RRS feed

  • Question

  • What is the best way to log to the Event Log from my WCF service? Previously, I've used log4net inside my .asmx's to do this, but now I'd like to leverage <system.servicemodel/> and <system.diagnostics/> from web.config where possible. I basically need to log.Info(...) and log.Error(...) inside my service implementation.

     

    Thanks,

    Mark

    Sunday, March 26, 2006 11:17 PM

Answers

  • Mark,

    Here are the steps:

     

    1.       Define a trace source for your service in user code, using System.Diagnostics API.

    TraceSource logEvents = new TraceSource("EventLogTraceSource");

    Using that trace source, trace events at the right level:

    logEvents.TraceEvent(TraceEventType.Warning, 667, "event data");

     

    I appended below a table describing the trace levels and corresponding events in WCF, for usage illustration. 

     

    System.Diagnostics API:

    http://msdn2.microsoft.com/en-us/library/system.diagnostics(VS.80).aspx

     

     

    2. Set the level for your trace source in config

    <source name="EventLogTraceSource" switchValue="Warning">

    All events of the trace level you specify as well as of the above levels will be traced.

    Example:

    Warning specified => Warning, Error, Critical events are traced

    Information specified => Information, and Warning and up events are traced

    3. Also add the System.Diagnostics' EventLogTraceListener listener to your trace source in config

                  <add type="System.Diagnostics.EventLogTraceListener" name="eventLogListener"/>

    http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlogtracelistener(VS.80).aspx

     

    Here is a full example:

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

      <system.diagnostics>

        <sources>

          <source name="EventLogTraceSource" switchValue="Warning">

            <listeners>

              <add name="eventLogListener">

              </add>

            </listeners>

          </source>

        </sources>

        <sharedListeners>

          <add   type="System.Diagnostics.EventLogTraceListener" name="eventLogListener"/>

        </sharedListeners>

        <trace autoflush="true" />

      </system.diagnostics>

    <system.serviceModel>

    ...

    </configuration>

    4. If you also would like to view your traces in the trace viewer, please add the XMWriterTraceListener listener to your config:

             <listeners>

              <add name="eventLogListener">

              </add>

              <add name="xmlListener">

              </add>

            </listeners>

          </source>

        </sources>

        <sharedListeners>

          <add   type="System.Diagnostics.EventLogTraceListener" name="eventLogListener"/>

          <add name="xmlListener"  type="System.Diagnostics.XMLWriteTraceListener"/>

        </sharedListeners>

     

    Thanks,

    Laurence

      

    Trace Level

    Nature of the tracked events

    What the tracked events capture

    Tracked events

    User Target

    Off

     

    N/A

    None

    N/A

    Critical.

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    “Negative” events: events that mark a bad processing or an error condition

    App cannot start or ceases to function.

     

    Unhandled exceptions:

    • OutOfMemoryException
    • ThreadAbortException (the CLR invokes any ThreadAbortExceptionHanler)
    • StackOverflowException (cannot be caught)
    • ConfigurationErrorsException
    • SEHException

    Start errors

    Failfast events

    System hangs

    Poison messages: message traces that cause the application to fail.

    In order:

    Admins

    App developers

    PSS

    Product developers devs

    Error

     

    Bad processing happened: the application was not able to perform a task as expected. However, the app is still up and running

    Caught exceptions. The Tracing ThrowHelper function is used by feature teams to generate an error trace on these exceptions.

    Example: a request message cannot be processed, the WCF server throws an exception; when it is caught, thr server sends a soap fault message to the client.

    Admins

    App developers

    PSS

    Product developers

    Warning

     

    A possible problem occurred or may occur. The expected processing happened but there are indications that bad processing could farther happen.

     

    Timeout exceeded

    Limits exceeded (e.g., limit of concurrent connections)

    Rejected credentials

    Admins

    App developers

    PSS

    Product developers

    Information

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    “Positive” events: events that mark successful milestones

    This level indicates important successful miletones, independently of the app working or not properly.

    Examples:

    Created channel,

    Created endpoint listener

    Message enters/leaves transport

    Message enters/leaves user code

    Retrieved security token

    Admins

    App developers

    PSS

    Product developers

     

    For validation during deployment or production – perf permitting

    Verbose

     

    Low level events are emitted.

    Examples:

    Message encoded/decoded

    Message logged

    Understood message header …

     

    App developers

    PSS

    Product developers

    , for debugging or app optimization

    ActivityTracing

     

    Flow events between processing activities and components

    Traces for activity boundaries – start/stop

    Traces for transfers

    All

    All

     

    App may function properly. All events are emitted.

    All events above.

     

     

    The levels from Verbose to Critical are stacked on top of each other: each trace level includes all levels above it. For example, a listener listening at the warning level receives critical, error, and warning traces.

    The All level includes events form Verbose to Critical as well as Activity tracing events.

     

    Tuesday, April 4, 2006 12:31 AM