Search code examples
c#traceevent-log

Writing to event log by using data from config file


I am doing some practice tests for Microsoft's "Programming in C#" exam 70-483 and one of the questions has me in a stump and I don't know enough about tracing to test it properly.

The question is this:

You are developing an application that uses a .config file.
The relevant portion of the .config file is shown as follows:

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

You need to ensure that diagnostic data for the application writes to the event log by using the configuration specified in the .config file. What should you include in the application code?

A. EventLog log = new EventLog(); <br/>log.WriteEntry("Trace data...");

B. Debug.WriteLine("Trace data...");

C. Console.SetOut(new StreamWriter("System.Diagnostics.EventLogTraceListener")); <br/>Console.WriteLine("Trace data...");

D. Trace.WriteLine("Trace data...");

I'm thinking "C" because it's the only option that has something to do with the .config file, but it says the correct answer is "D" for some reason. Mind you these questions have been wrong before about completely simple and obvious stuff, so...

Can you please explain which is the correct answer and why?


Solution

  • The solution is to read about tracing instead of trying to just find the correct answer.

    C is the absolute worst - not only does it have nothing to do with System.Diagnostics or the EventLog, it will try to create a file named System.Diagnostics.EventLogTraceListener.

    The only options that actually use System.Diagnostics are B and D.

    The <system.diagnostics><trace> section in app.config configures the System.Diagnostics classes, including sources, listeners, switches that activate or deactive specific sources in the application etc. It's no different really than the configuration files used for any other log library like log4net, Serilog or NLog.

    • Debug.WriteLine("Trace data..."); will write the output to all the configured trace listeners, only in a debug build.

    • Trace.WriteLine("Trace data..."); will write to all configured trace listeners both in debug and release builds.

    Option A is wrong too. It does involve the event log, but won't work because the Source isn't set. EventLog.WriteEntry doesn't send any diagnostic information either, it just writes an Information message to the Event Log.

    The System.Diagnostics listener on the other hand will receive diagnostic events from the myriad of trace sources in .NET, including the application's own, filter it, and write it to its target.

    Do you have a problem with networking for example? You can switch the System.Net trace listeners on and collect trace messages from the HTTP level all the way down to sockets, SSL, TCP and even packet operations.