Why call stack info is absent for my EventLogTraceListener instance?

306 views Asked by At

C# 6.0, .NET Framework 4.6.1, Debug mode.

I set the TraceOptions.Callstack value for my EventLogTraceListener instance, but I don't see the call stack in the Windows Event Viewer.

This is my simple code:

public static string GetMsgHeaders() {

    string currentMethodName = MethodBase
        .GetCurrentMethod().Name;

    Console.Out.WriteLine("The {0} method was called!",
        currentMethodName);

    if (!EventLog.SourceExists("sharp_sandbox")) {
        EventLog.CreateEventSource("sharp_sandbox",
            "Application");
    }

    EventLogTraceListener listener = new
        EventLogTraceListener("sharp_sandbox");

    listener.TraceOutputOptions = TraceOptions.DateTime
        | TraceOptions.Callstack;

    Trace.Listeners.Add(listener);
    Trace.Write("Trace.Write");
    Trace.WriteLine("Trace.WriteLine");
    Trace.TraceWarning("Trace.TraceWarning");
    Trace.TraceError("Trace.TraceError");
    Trace.TraceInformation("Trace.TraceInformation");

    listener.Close();

    return "ABCDEF";
}

But I get info without call stack, for example:

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
    <System>
        <Provider Name="sharp_sandbox" /> 
        <EventID Qualifiers="0">0</EventID> 
        <Level>4</Level> 
        <Task>0</Task> 
        <Keywords>0x80000000000000</Keywords> 
        <TimeCreated SystemTime="2016-08-17T09:48:40.367843800Z" /> 
        <EventRecordID>13778</EventRecordID> 
        <Channel>Application</Channel> 
        <Computer>Win10x64-VS.spb.gpsm.ru</Computer> 
        <Security /> 
    </System>
    <EventData>
        <Data>Trace.TraceInformation</Data> 
    </EventData>
</Event>

Why call stack info is absent?

2

There are 2 answers

0
Serge V. On

All you need is to add a simple listener that directs tracing or debugging output to an EventLog. Insert <system.diagnostics> config somewhere in between <configuration> </configuration> in your App.config file.

<system.diagnostics>
      <trace autoflush="false" indentsize="4">
        <listeners>
          <add name="myListener"
            type="System.Diagnostics.EventLogTraceListener"
            initializeData="TraceListenerLog" />
        </listeners>
      </trace>
 </system.diagnostics>

This code above adds a EventLogTraceListener object named myListener to the Trace.Listeners collection. The initializeData parameter specifies the name of the event log source that is to be passed to the EventLogTraceListener(String) constructor. For more details google for EventLogTraceListener class.

0
wavydavy On

Unfortunately the Trace Options are not used by the EventLogTraceListener. It Claims that it would spit out too much data!

https://msdn.microsoft.com/en-us/library/a10k7w6c(v=vs.110).aspx

Stated in the Remarks section.