流畅的配置问题

时间:2010-07-07 12:59:19

标签: .net logging enterprise-library fluent

我正试图让流畅的配置工作而没有成功。我收到以下错误:

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

当我尝试访问logwriter时:

Dim logwriter As LogWriter = EnterpriseLibraryContainer.Current.GetInstance(Of LogWriter)()

配置:

    Dim formatBuilder As New FormatterBuilder()
    Dim builder As New ConfigurationSourceBuilder()
    builder.ConfigureInstrumentation().EnableLogging()
    builder.ConfigureLogging.LogToCategoryNamed("Important") _
        .WithOptions.SetAsDefaultCategory() _
        .SendTo.RollingFile("StandardListener") _
                .RollEvery(RollInterval.Midnight) _
                .RollAfterSize(50000) _
                .WhenRollFileExists(RollFileExistsBehavior.Increment) _
        .FormatWith(formatBuilder.CustomFormatterNamed("StandardFormatter", GetType(StandardFormatter))) _
        .ToFile("D:\LogFiles\" + fileName)

更新

我添加了

        var configSource = new DictionaryConfigurationSource();
        builder.UpdateConfigurationWithReplace(configSource);
        EnterpriseLibraryContainer.Current
          = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

我仍然无法获得一个编写器,而且LAB中的Logger也无法获得它(它在内部使用EnterpriseLibraryContainer.Current.GetInstance<LogWriter>());

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

完成配置后,您对配置实际做了什么?您是否已将其放入配置源并将其提供给容器或Entlib?

乍一看,您的配置看起来不错,但这只是构建对象图。之后你需要这样的东西:

  dim configSource as new DictionaryConfigurationSource()
  builder.UpdateConfigurationSourceWithReplace(configSource)
  ' To use container directly
  dim container as IUnityContainer = new UnityContainer() _
    .AddExtension(New EnterpriseLibraryCoreExtension(configSource))

  ' Or, to use Entlib static APIs
  EnterpriseLibraryContainer.Current = EnterpriseLibrary.CreateDefaultContainer(configSource)

应该这样做。如果没有,请发布更详细的示例,我可以为您调试。

<强>更新

以下示例对我来说效果很好,我只是尝试过:

Imports Microsoft.Practices.EnterpriseLibrary.Logging
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners

Module Module1

    Sub Main()
        InitializeEntlib("test.log")

        Logger.Write("This is a test message")
    End Sub

    Sub InitializeEntlib(ByVal logFileName As String)
        Dim builder As New ConfigurationSourceBuilder
        builder.ConfigureLogging() _
        .LogToCategoryNamed("Important") _
        .WithOptions.SetAsDefaultCategory() _
        .SendTo _
            .RollingFile("StandardListener") _
                .RollEvery(RollInterval.Midnight) _
                .RollAfterSize(50000) _
                .WhenRollFileExists(RollFileExistsBehavior.Increment) _
            .FormatWith(New FormatterBuilder().TextFormatterNamed("Text Formatter")) _
            .ToFile("D:\LogFiles\" + logFileName)

        Dim configSource As New DictionaryConfigurationSource()
        builder.UpdateConfigurationWithReplace(configSource)

        EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource)
    End Sub
End Module

我能看到的唯一不同的是我使用的是TextFormatter而不是你的StandardFormatter。尝试一下,看看它是否有效 - 如果没有,那么还有其他的东西缺失。

答案 1 :(得分:0)

我遇到了类似的错误。这是因为配置不正确。我使用了FormatWithSharedFormatter,但没有定义共享格式化程序。

我没有抱怨过:

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

或:

EnterpriseLibraryContainer.Current
    = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

我得到的只是在尝试从Unity中检索时未找到记录器。删除FormatWithSharedFormatter配置使一切正常。