我们将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>
有人解决了这个吗?
答案 0 :(得分:2)
您提到当有关于无效权限的问题时,您会看到此行为;虽然没有任何关于此的具体文档, 信息围绕读/写访问到文件:
问题似乎是Enterprise Library不考虑无法写入日志文件,因为它只读为错误。这在MSDN的以下文档中说明:
表4:Flatfile TraceListener属性
注意:强>
如果您指定的文件为 FlatfileTraceListener是只读的, 跟踪侦听器不写入 数据到文件,没有例外 发生。确保该文件 属性设置为读/写。
因为它不认为它是一个错误,它不会被特殊类别“Logging Errors&amp; Warnings”拾取,因此不会被写入事件日志(在您的情况下)。
虽然文档没有明确说明权限问题会发生同样的情况,但我已经厌倦了使用你的配置文件(只读,没有权限),并且表现出同样的行为。
不幸的是,您似乎没什么选择,只能确保文件可写,并且执行应用程序的用户具有正确的写入权限。