你好呀,
我刚刚设置了 Enterprise Library 5 的日志记录应用程序块部分。我认为我已经完成了正确的操作,但它没有记录到事件日志,它在写入方法上出错并给出了以下异常。
The type LogWriter cannot be constructed.
You must configure the container to supply this value.
谁能帮我检查一下或告诉我我缺少什么..
首先,我在 Windows 7 中运行 winforms 应用程序。
这是我创建日志的方法,您可以看到 write 方法。
public class Logger : ILogger
{
public void SendTest(string test)
{
LogEntry log = new LogEntry();
log.EventId = 300;
log.Message = test;
log.Categories.Add("testing");
log.Severity = TraceEventType.Information;
log.Priority = 5;
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(log);
}
}
我的所有配置都是使用 entlib5 编辑器构建的,而不是手动构建的。并确认 si entlib.config 所在的路径
filePath="C:\myapp\entlib.config" />
这是我的 app.config,它指向我的 entlib.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
<sources>
<add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="File-based Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
filePath="C:\myapp\entlib.config" />
</sources>
<redirectSections>
<add sourceName="File-based Configuration Source" name="Redirected Section" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
这是我的 entlib.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
source="Enterprise Library Logging" formatter="Text Formatter"
log="" machineName="." traceOutputOptions="None" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp}{newline}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Event Log Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
EDIT该问题已使用新的 app.config 进行了更新,包括
<redirectSections>
<add sourceName="File-based Configuration Source" name="Redirected Section" />
</redirectSections>
我仍然收到错误。
Activation error occured while trying to get instance of type
日志写入器,键“”
和内部异常是
依赖关系解析失败,
类型=
\"Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter\",
名称 = \"(无)\"。\r\n异常
发生时间:同时
正在解决。\r\n异常是:
InvalidOperationException - 类型
无法构造 LogWriter。你
必须配置容器来供应
这
值。\r\n-------------------------------------------------------- ---\r\n异常发生时,
容器为:\r\n\r\n 正在解析
Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(无)\r\n"}
我使用 EntLib 配置器创建这两个文件。
我接受了默认的 entlib.config,因为它似乎包含了 EventLog 侦听器。
EDIT
这是我在 entlib.config 文件中的行,日志为空 - 呃,这是正确的吗?
<add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
source="Enterprise Library Logging" formatter="Text Formatter"
log="" machineName="." traceOutputOptions="None" />
EDIT
我的 Logger.write 在 dll 中,但我的 app.config / entlib.config 在 winforms 应用程序中。winforms 应用程序引用了我在其中写入日志的 dll 项目。
dll和winforms都有参考
Microsoft.Practices.EnterpriseLibrary.Logging
并且还使用统一,具有
Microsoft.Practices.Unity
Microsoft.Practices.Unity.Interception