尝试获取 LogWriter 类型的实例时发生激活错误

发布于 2024-09-02 18:22:38 字数 3048 浏览 8 评论 0原文

我正在尝试使用 Enterprise Library 5.0 的日志应用程序块将简单的消息记录到 Win XP SP3 系统上的 Windows 事件日志中,方法是:

Logger.Write(msg);

尝试获取 LogWriter 类型的实例时出现“激活错误”错误消息日志。

下面显示的是与 MS Enterprise 库一起使用的配置文件

<?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="Application" 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>

I am trying to using the Logging Application block of Enterprise Library 5.0 to log simple message to the Windows event log on Win XP SP3 system using:

Logger.Write(msg);

I get the "Activation error occured while trying to get instance of type LogWriter" error message when trying to log.

Shown below is the config file used with MS Enterprise library

<?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="Application" 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

活泼老夫 2024-09-09 18:22:39

另外示例
西蒙·图西
我想添加我的示例,当日志记录配置位于单独的文件中但 app.config 中缺少部分描述

时 

<section name ="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />  

 <loggingConfiguration configSource="EnterpriseLibraryLogging.config" />

Additionally to examples of
Simon Tewsi
 I want to add my example, when logging configuration was located in a separate file but section descriptions were missed in app.config

<section name ="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />  

 <loggingConfiguration configSource="EnterpriseLibraryLogging.config" />
埋葬我深情 2024-09-09 18:22:38

我只是想添加此错误可能是由另一个配置问题引起的。请务必查看此错误的内部异常。就我而言,它是:

“无法构造数据库类型。您必须配置容器以提供此值。”

要解决此问题,我必须向我的数据库添加一个providerName web.config 中的数据库连接字符串。所以最终的连接字符串节点如下所示:

 <add name="DBConn" connectionString="Data Source=ServerName;Initial Catalog=Logging;Persist Security Info=True;integrated security=True;" providerName="System.Data.SqlClient" />

I just wanted to add this error may be caused by another configuration issue. Make sure to look at the inner exceptions for this error. In my case it was:

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

To resolve this I had to add a providerName to my database connection string in the web.config. So the final connection string node looked like this:

 <add name="DBConn" connectionString="Data Source=ServerName;Initial Catalog=Logging;Persist Security Info=True;integrated security=True;" providerName="System.Data.SqlClient" />
飘过的浮云 2024-09-09 18:22:38

我意识到我试图使用 DLL 中的配置文件,但它不起作用。我应该改用 FileConfigurationSource 。

如果我在应用程序中使用相同的 App.Config,则效果很好。

I realized that I was trying to use Config file from within a DLL which does not work. I should be using FileConfigurationSource instead.

If I use the same App.Config from an application, it worked fine.

巴黎盛开的樱花 2024-09-09 18:22:38

阅读此问题的其他答案,并根据我自己的经验,一般来说,当您的应用程序无法从配置文件中读取日志记录应用程序块所需的某些配置时,似乎会发生此错误。

添加到前面的答案中提到的场景中,我多次遇到此错误:

1)在单元测试项目中,我根本忘记添加 app.config 文件;

2)在配置文件中,我从loggingConfiguration部分删除了特定的侦听器,但忘记从引用它的categorySource中删除侦听器的名称。

Reading the other answers to this question, and from my own experiences, in general it seems this error occurs when your application cannot read some required configuration for the Logging application block from a config file.

To add to the scenarios mentioned in previous answers, I've come across this error a couple of times:

1) In a unit test project, where I forgot to add an app.config file at all;

2) In a config file where I deleted a particular listener from the loggingConfiguration section but forgot to remove the name of the listener from the categorySource that referenced it.

¢蛋碎的人ぎ生 2024-09-09 18:22:38

缺少 DLL;当您将 DLL 放入 GAC 时请注意,您可能需要向 GAC 添加更多 DLL。 Common、Data、Logging、Logging.Database 和 ServiceLocation DLLS 确保它们一起驻留在一个目录中

It is missing DLL; pay attension when you place your DLL's at GAC you may need to add more DLL's to GAC . Common, Data, Logging, Logging.Database and ServiceLocation DLLS make sure they reside together in one directory

夏日落 2024-09-09 18:22:38

当忘记添加对 EntLib DLL 的引用时,我也看到了此错误。

I've also seen this error when forgetting to add a reference to the EntLib DLL's.

遇见了你 2024-09-09 18:22:38

使用单独的 entlib 配置文件时我遇到了同样的错误。

在Web.config中,enterpriseLibrary.ConfigurationSource指向EntLib.config。当我使用 EnterpriseLibrary.Config 工具编辑 EntLib.config 以设置日志记录数据库详细信息时,它将所有内容都放在 EntLib.config 中。在将 connetionStrings 部分移至 Web.config 之前,我遇到了此错误。

I've had the same error when using a separate entlib config file.

In Web.config, enterpriseLibrary.ConfigurationSource pointed to EntLib.config. When I used the EnterpriseLibrary.Config tool to edit EntLib.config to set up the logging database details, it put everything in EntLib.config. I got this error until I moved the connetionStrings section to Web.config.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文