gpt4 book ai didi

.net - EntLib5 : Loggin application block not logging to Event Log (exception: type LogWriter cannot be constructed)- Can anyone help?

转载 作者:行者123 更新时间:2023-12-04 15:37:03 26 4
gpt4 key购买 nike

你好呀,

我只是设置了 Enterprise Library 5 的日志记录应用程序块部分。我认为我已经做对了,但它没有记录到事件中,它在 write 方法上记录了错误,并给了我以下异常。

    The type LogWriter cannot be constructed. 
You must configure the container to supply this value.

任何人都可以帮我检查或告诉我我错过了什么..

首先,我在带有 winforms 应用程序的 Windows 7 中运行。

这是我创建日志的方法,您会看到 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}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;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 &amp; Warnings">
<listeners>
<add name="Event Log Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>

编辑
问题已更新为新的 app.config,包括
<redirectSections>
<add sourceName="File-based Configuration Source" name="Redirected Section" />
</redirectSections>

我仍然收到错误。

 Activation error occured while trying to get instance of type

LogWriter, key ""



内部异常是

Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(none)\r\n"}



我使用 EntLib 配置器来创建这两个文件。

entlib.config 我接受了默认值,因为它似乎包含了 EventLog 监听器。

编辑

这是我在 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" />

编辑

我的 Logger.write 在 dll 中,但我的 app.config/entlib.config 在 winforms 应用程序中。winforms 应用程序引用了我在其中写入日志的 dll 项目。

dll 和 winforms 都引用了
   Microsoft.Practices.EnterpriseLibrary.Logging

并且还使用了 unity,有
   Microsoft.Practices.Unity

Microsoft.Practices.Unity.Interception

最佳答案

Martin,您已经创建了一个基于文件的配置源,但您还没有将日志记录部分重定向到它。

将以下内容添加到enterpriseLibrary.ConfigurationSource节点内的App.config:

<redirectSections>
<add sourceName="File-based Configuration Source" name="loggingConfiguration" />
</redirectSections>

另外,我推荐来自 Enterprise Library 5.0 Hands-on Labs 的 Configuration Sources Labs放。它涵盖了这个和更高级的场景。

关于.net - EntLib5 : Loggin application block not logging to Event Log (exception: type LogWriter cannot be constructed)- Can anyone help?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5521675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com