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?
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.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.