【问题标题】:Enterprise Library Logging : Activation error occured while trying to get instance of type LogWriter, key ""企业库日志记录:尝试获取 LogWriter 类型的实例时发生激活错误,密钥“”
【发布时间】:2013-01-08 13:44:33
【问题描述】:

我正在尝试使用 Fluent API 在没有配置文件的情况下编写日志。

这是我的代码:

 var builder = new ConfigurationSourceBuilder();
                builder.ConfigureData()
                .ForDatabaseNamed("Logging")
                .ThatIs.ASqlDatabase()
                .WithConnectionString(ConnectionString)
                .AsDefault();

 var configSource = new DictionaryConfigurationSource();
 builder.UpdateConfigurationWithReplace(configSource);

 var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

 logger.Write("Test", "General");

当我运行它时,它只是说

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

我在配置中做错了什么?

【问题讨论】:

    标签: logging configuration enterprise-library


    【解决方案1】:

    缺少两件事:

    1. 您忘记使用EnterpriseLibraryContainer.CreateDefaultContainer 方法使用配置更新容器
    2. 您正在配置数据访问,但正在尝试使用日志记录块

    以下是使用配置信息创建容器的方法:

    var builder = new ConfigurationSourceBuilder();
                    builder.ConfigureData()
                    .ForDatabaseNamed("Logging")
                    .ThatIs.ASqlDatabase()
                    .WithConnectionString(ConnectionString)
                    .AsDefault();
    
     var configSource = new DictionaryConfigurationSource();
     builder.UpdateConfigurationWithReplace(configSource);
    
     EnterpriseLibraryContainer.Current 
      = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    

    要使用 Fluent API 配置日志记录,请参阅 Using the Fluent Configuration API

    那篇文章的例子是:

    var builder = new ConfigurationSourceBuilder();
    
    builder.ConfigureLogging()
           .WithOptions
             .DoNotRevertImpersonation()
           .LogToCategoryNamed("Security")
             .SendTo.FlatFile("Security Log File")
               .FormatWith(new FormatterBuilder()
                 .TextFormatterNamed("Text Formatter")
                   .UsingTemplate("Timestamp: {timestamp}...{newline})}"))
                 .ToFile("security.log")
             .SendTo.EventLog("Formatted EventLog TraceListener")
                .FormatWithSharedFormatter("Text Formatter")
                  .ToLog("Application")
           .LogToCategoryNamed("General")
             .WithOptions.SetAsDefaultCategory()
             .SendTo.SharedListenerNamed("Formatted EventLog TraceListener");
    
    var configSource = new DictionaryConfigurationSource();
    builder.UpdateConfigurationWithReplace(configSource);
    EnterpriseLibraryContainer.Current 
      = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    

    如果您希望将日志记录块配置为使用数据访问块写入数据库,您也可以使用流利的界面来完成:

    var builder = new ConfigurationSourceBuilder();
    
    builder.ConfigureData()
        .ForDatabaseNamed("Logging")
            .ThatIs.ASqlDatabase()
            .WithConnectionString(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;Database=Logging")
        .AsDefault();
    
    builder.ConfigureLogging()
            .WithOptions
                .DoNotRevertImpersonation()
            .LogToCategoryNamed("General")
                .WithOptions.SetAsDefaultCategory()
                .SendTo.Database("Database Trace Listener")
                .WithAddCategoryStoredProcedure("AddCategory")
                .UseDatabase("Logging")
                .Filter(System.Diagnostics.SourceLevels.All)
                .WithWriteLogStoredProcedure("WriteLog");
    
    
    var configSource = new DictionaryConfigurationSource();
    builder.UpdateConfigurationWithReplace(configSource);
    EnterpriseLibraryContainer.Current
        = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    
    var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
    logger.Write(logEntry);
    Logger.Write("Test", "General");
    

    【讨论】:

    • 是的,非常感谢......你做了很好的笔记......但问题是我无法理解配置 Logging 块的复杂性。这个来自 MSDN 的示例代码只是展示了如何写入事件日志而不是数据库。我找不到任何显示如何使用 fluent API 配置日志记录的来源
    • 我第二点,你太棒了。
    • 很棒的帖子。谢谢。但我认为它也有同样的问题。 logger 什么时候卸载?
    猜你喜欢
    • 2014-07-22
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多