【发布时间】:2010-10-15 20:46:58
【问题描述】:
我最近创建了一个 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 中的东西的用途是什么?
【问题讨论】:
标签: .net configuration windows-services event-log