.net – 当文件不可访问时,Enterprise Library Logging 4.1不会写入错误侦听器

我们将Enterprise Library 4.1日志记录设置为在常规日志记录失败时写入事件日志.当日志配置不正确时(例如,侦听器fileName属性中的字符无效),这可以正常工作,但是由于权限无效而无法写入配置的文件时,不会将任何内容写入事件日志.

FWIW这里是配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add source="Enterprise Library Logging" formatter="Event Log Formatter" machineName="."
           log="Application"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           traceOutputOptions="None" filter="All"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           name="Formatted EventLog TraceListener" />
      <add formatter="Text Formatter"
        fileName="d:\trace.log" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text TraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Event Log Formatter" />
      <add template="[{timestamp(yyyy-MM-dd HH:mm:ss,fff)}] {severity} {category}: {message}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Text TraceListener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>

有人解决了这个吗?

最佳答案 您提到在有关无效权限的问题时您会看到此行为;虽然没有任何关于此的具体文档,但有关于文件的读/写访问的信息:

问题似乎是Enterprise Library不考虑无法写入日志文件,因为它只读为错误.这在MSDN的以下文档中说明:

Table 4: Flatfile TraceListener Properties

Note:

If the file you specify for the
FlatfileTraceListener is read-only,
the trace listener does not write the
data to the file and no exception
occurs
. Make sure that the file
attributes are set to read/write.

因此,它不会将其视为错误,它不会被特殊类别“记录错误和警告”所吸收,因此不会写入事件日志(在您的情况下).

虽然文档没有明确说明权限问题会发生同样的情况,但我已经厌倦了使用您的配置文件(只读,并且没有权限),并且表现出相同的行为.

不幸的是,似乎你没有什么选择,只能确保文件是可写的,并且执行应用程序的用户具有写入它的正确权限.

点赞