views:

23

answers:

1

I recently created a Windows service. One thing that it faithfully does is log any errors to the Application log. I have the following code that does this:

Dim appLog = New System.Diagnostics.EventLog With {.Source = "MyService"}
appLog.WriteEntry(message, EventLogEntryType.Error, transactionID)

I also have the following in my app.config:

<system.diagnostics>
    <sources>
      <source name="MyService" switchName="DefaultSwitch">
        <listeners>
          <!--<add name="FileLog"/>-->          
          <add name="EventLog"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <!--<add name="FileLog"
           type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
           initializeData="FileLogWriter"/>-->      
      <add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="MyService"/> 
    </sharedListeners>
  </system.diagnostics>

I would expect that my code above should work even without setting the Source property of EventLog programmatically because I've already defined the source in the config file. But if I remove With {.Source = "MyService"}, then I get an Exception, which that says that the Source property should be set before calling WriteEntry method. So, what's purpose of the stuff in the configuration XML?