none
Event log trace listener to log messages to custom event source RRS feed

  • Question

  • I've got an EventLogTraceListener set up in my code and currently configured to write to the default Windows event log, in this case the Application log.  I've created a new event log source and I want to be able to write my messages to the new event log source and not the default (application log).  I've seen a couple of examples that pass the log name into the initializedata attribute of the tracelistener, but I'm not able to get this to work.  Any help would be greatly appreciated.  Here is a snippet from my app.config:

    <system.diagnostics>
        <sources>
          <source name="RoutingServiceSource" switchName="RoutingServiceSourceSwitch" switchType="System.Diagnostics.SourceSwitch">
            <listeners>
              <!--<add name="RoutingServiceListener" type="System.Diagnostics.EventLogTraceListener" initializeData="Log=AGOC;Source=AGOC.Routing.Outbound">-->
              <add name="RoutingServiceListener" type="System.Diagnostics.EventLogTraceListener" initializeData="RoutingService">
                <!--<filter type="System.Diagnostics.EventTypeFilter" initializeData="Error"/>-->
              </add>
            </listeners>
          </source>
        </sources>
        <!--<trace autoflush="false" indentsize="4">
          <listeners>
            <add name="RoutingServiceListener" type="System.Diagnostics.EventLogTraceListener" initializeData="RoutingService">
            </add>
          </listeners>
        </trace>-->
        <!--Switch values:
            Error=Output error-handling messages.
            Info=Output informational messages, warnings, and error-handling messages.
            Off=Output no tracing and debugging messages.
            Verbose=Output all debugging and tracing messages.
            Warning=Output warnings and error-handling messages.-->
        <switches>
          <add name="RoutingServiceSourceSwitch" value="Error" />
        </switches>
      </system.diagnostics>

    Tuesday, December 10, 2013 3:56 PM

Answers

  • Hello Kenny,

    Writing to a different source can be tricky because there differences between operating systems (os server versions) and permissions.

    I am guessing but this could be because you need to create the application source explictly and associate RoutingServiceSource to the RoutingService eventlog.  Below is an example from MSDN and is performed at the startup of the service or program (note: from memory server 2003 did not require this but newer server versions do):

     // Create the source, if it does not already exist. 
            if(!EventLog.SourceExists("MySource"))
            {
                // An event log source should not be created and immediately used. 
                // There is a latency time to enable the source, it should be created 
                // prior to executing the application that uses the source. 
                // Execute this sample a second time to use the new source.
                EventLog.CreateEventSource("MySource", "MyNewLog");
                Console.WriteLine("CreatingEventSource");
                Console.WriteLine("Exiting, execute the application a second time to use the source.");
                // The source is created.  Exit the application to allow it to be registered. 
                return;
            }
    
            // Create an EventLog instance and assign its source.
            EventLog myLog = new EventLog();
            myLog.Source = "MySource";
    
            // Write an informational entry to the event log.    
            myLog.WriteEntry("Writing to event log.");
    
            Console.WriteLine("Message written to event log.");  
    These might help:

    http://msdn.microsoft.com/en-us/library/5zbwd3s3(v=vs.110).aspx

    http://msdn.microsoft.com/en-us/library/49dwckkz(v=vs.90).aspx


    Jeff

    Tuesday, December 10, 2013 10:25 PM