正确使用应用程序配置文件中的设置

发布于 2024-09-28 08:42:53 字数 1371 浏览 0 评论 0原文

我最近创建了一个 Windows 服务。它忠实地做的一件事是将所有错误记录到应用程序日志中。我有以下代码可以执行此操作:

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

我的 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>

即使不以编程方式设置 EventLog 的 Source 属性,我也希望上面的代码也能正常工作,因为我已经在配置文件中定义了源。但是,如果我删除 With {.Source = "MyService"},则会收到异常,这表明应在调用 WriteEntry 方法之前设置 Source 属性。那么,配置 XML 中的内容的用途是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2024-10-05 08:42:53

幸运的是,我在开发另一个应用程序时偶然发现了一个答案,或者至少是部分答案。使用 My.Application.Log 对象时,配置文件中的设置很重要。例如:My.Application.Log.WriteEntry(errMsg) 将根据 app.config 中的设置写入文件或事件查看器。

因此,在这种情况下,不会直接从代码调用 EventLog 类。这为我提供了额外的灵活性,可以将日志格式从事件查看器“热交换”到文件,例如,无需更改任何代码或重新编译。但我确实注意到的一件事是 元素的 Name 属性需要设置为“DefaultSource”。否则,事件不会被记录。可能有一种方法可以改变这一点,但我认为没有必要,所以我没有进一步调查。希望这对某人有用。

As luck has it, I've stumbled on an answer, or at least a partial one while working on a different app. The settings in the config file matter when using My.Application.Log object. For example: My.Application.Log.WriteEntry(errMsg) will write to a file or to the Event Viewer depending on the setting in app.config.

So, in this case, the EventLog class is not called directly from code. This gives me the extra flexibility to "hot-swap" the log format from Event Viewer to file, for example, without changing any code or recompiling. One thing that I did notice though is that the Name property of the <source> element needs to be set to "DefaultSource". Otherwise, events don't get logged. There's probably a way to change that, but I didn't see a need, so I didn't investigate further. Hopefully, this will be useful to somebody.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文