Question

I am trying to use ETW to log errors/exceptions in my service.

The event source is pretty basic:

public class Logger : EventSource
{
    public static readonly Logger Instance = new Logger();

    // Use default trace listener so only profiler sees this data
    // it will not be part of Tako

    private Logger()
    {
        // Do nothing, but make the constuct private so static singleton use is enforced
    }

    [Event(EventId.StoreValueException, Level = EventLevel.Critical)]
    public void CriticalException(Exception ex, int eventId)
    {
        WriteEvent(eventId, ex);
    }

    [Event(EventId.StoreValueException, Level = EventLevel.Error)]
    public void Exception(Exception ex, int eventId)
    {
        WriteEvent(eventId, ex);
    }

    [Event(EventId.StoreValueException, Level = EventLevel.Warning)]
    internal void Warning(string p, params object[] paramList)
    {
        WriteEvent(EventId.StoreValueLog, String.Format(p, paramList));
    }

    [Event(EventId.StoreValueException, Level = EventLevel.Informational)]
    public void Info(string p, params object[] paramList)
    {
        WriteEvent(EventId.StoreValueLog, String.Format(p, paramList));
    }

    [Event(EventId.StoreValueException, Level = EventLevel.Verbose)]
    public void Verbose(string p, params object[] paramList)
    {
        WriteEvent(EventId.StoreValueLog, String.Format(p, paramList));
    }
}

Now I am trying to use that source to log to a file:

<system.diagnostics>
<sources>
  <source name="MyNameSpace" switchValue="All" >
    <listeners>
      <add name="ErrorLoggingListener">
        <filter type="" />
      </add>
    </listeners>
  </source>
  <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
    <listeners>
      <add name="ServiceModelMessageLoggingListener">
        <filter type="" />
      </add>
    </listeners>
  </source>
</sources>
<sharedListeners>
  <add initializeData="C:\ap\app\test\logs\fullweb.log"
      type="System.Diagnostics.TextWriterTraceListener" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
    <filter type="" />
  </add>
  <add initializeData="C:\ap\app\test\logs\errors.log"
      type="System.Diagnostics.TextWriterTraceListener" name="ErrorLoggingListener" traceOutputOptions="Timestamp">
    <filter type="" />
  </add>
</sharedListeners>
</system.diagnostics>
<system.serviceModel>
<diagnostics performanceCounters="All" >
  <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="false" />
</diagnostics>
</system.serviceModel>

So the system.serviceModel logging works, but my event source logging doesn't. I feel like I have to set something to tell the listener to log everything. SwitchValue=All doesn't seem to be doing the trick

Was it helpful?

Solution

Went with custom listener... Added this nuget package

<package id="Microsoft.Diagnostics.Tracing.TraceEvent" version="1.0.21" targetFramework="net45" />
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            string guid1 = "aad1e3a4-8f28-44c8-b7a3-31b9d3ae08d3";
            string guid2 = "8ac71c54-393d-411d-a93e-1c774ebe0cd3";
            string guid3 = "79ac5539-ea81-4704-9009-495ae6f4809b";
            string guid4 = "a1808acc-75ba-49a2-92db-ca5e663fe629";

            if (args.Length > 0) guid1 = args[0];
            if (args.Length > 1) guid2 = args[1];
            if (args.Length > 2) guid3 = args[2];
            if (args.Length > 3) guid4 = args[3];

            new Thread(new ThreadStart(delegate { GetTraces(guid1); })).Start();
            new Thread(new ThreadStart(delegate { GetTraces(guid2); })).Start();
            new Thread(new ThreadStart(delegate { GetTraces(guid3); })).Start();
            new Thread(new ThreadStart(delegate { GetTraces(guid4); })).Start();

            Console.ReadLine();

            Environment.Exit(0);
        }

        public static void GetTraces(string guid)
        { 
            using (var session = new TraceEventSession("TestSession_" + guid))         // Create a session to listen for events
            {
                session.Source.Dynamic.All += delegate(TraceEvent data)              // Set Source (stream of events) from session.  
                {                                                                    // Get dynamic parser (knows about EventSources) 
                    if (data.EventName.Contains("Manifest"))
                    {
                        return;
                    }

                    // Subscribe to all EventSource events
                    Console.WriteLine("GOT Event " + data);                          // Print each message as it comes in 
                };

                var eventSourceGuid = new Guid(guid); // Get the unique ID for the eventSouce. 
                session.EnableProvider(eventSourceGuid);                                               // Enable MyEventSource.
                session.Source.Process();                                                              // Wait for incoming events (forever).  
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top