【问题标题】:Activation error occured while trying to get instance of type LogWriter尝试获取 LogWriter 类型的实例时发生激活错误
【发布时间】:2011-02-23 10:21:03
【问题描述】:

我正在尝试使用 Enterprise Library 5.0 的 Logging Application 块将简单消息记录到 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>

【问题讨论】:

    标签: c# enterprise


    【解决方案1】:

    我意识到我试图从一个不起作用的 DLL 中使用配置文件。我应该改用 FileConfigurationSource。

    如果我在应用程序中使用相同的 App.Config,它就可以正常工作。

    【讨论】:

      【解决方案2】:

      我在忘记添加对 EntLib DLL 的引用时也看到了这个错误。

      【讨论】:

        【解决方案3】:

        我只是想添加这个错误可能是由另一个配置问题引起的。确保查看此错误的内部异常。在我的情况下是:

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

        为了解决这个问题,我必须在 web.config 中的数据库连接字符串中添加一个 providerName。所以最终的连接字符串节点是这样的:

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

        【讨论】:

        • 永远是最简单的事情,需要最长时间才能解决!!谢谢!
        • 如果可以的话,我会给你 10 票赞成。哎呀....谢谢。有一个伟大的耶稣受难日。
        • 非常感谢,我为这个问题尝试了很多与 GAC 相关的解决方案,但直到这个问题才奏效。
        • 最简单的事情往往需要很长时间才能解决,因为编码人员只进行正面测试,而不考虑软件在出现问题时应该如何表现!
        【解决方案4】:

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

        【讨论】:

        • 我尝试将这些 DLLS 放在 GAC 中,但仍然出现相同的错误,但是当我将它们放在应用程序 bin 中时;错误消失了;
        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

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

          为了补充之前答案中提到的场景,我多次遇到此错误:

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

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

          【讨论】:

            【解决方案7】:

            除了examplesSimon Tewsi 我想添加我的示例,当日志配置位于单独的文件中但 app.config 中缺少部分描述时

               

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-03-04
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多