我们将 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}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}" 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 & Warnings">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
有人解决了这个问题吗?
最佳答案
您提到当存在有关无效权限的问题时,您会看到这种行为;虽然没有关于此的任何特定文档,但有关于对文件的读/写访问的信息:
问题似乎是企业库不考虑无法写入日志文件,因为它是只读的错误。这在来自 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.
因此,因为它不认为它是错误,所以它不会被特殊类别“记录错误和警告”接收,因此不会写入事件日志(在您的情况下)。
虽然文档没有具体说明权限问题会发生同样的情况,但我已经厌倦了使用您的配置文件(分别为只读和无权限)并且表现出相同的行为。
不幸的是,您似乎别无选择,只能确保该文件是可写的,并且执行您的应用程序的用户具有正确的写入权限。
关于.net - 当文件无法访问时,企业库日志记录 4.1 不会写入错误监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3200596/