开发者

Fluent configuration problem

I'm trying to get fluent configuration working without success. I get the following error:

Activation error occured while trying to get instance of type LogWriter, key ""

when I try to access the logwriter:

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

Configuration:

    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)

Update

I've added

        var configSource = new DictionaryConfigurationSource(开发者_StackOverflow);
        builder.UpdateConfigurationWithReplace(configSource);
        EnterpriseLibraryContainer.Current
          = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

I still can't get a logwriter, and Logger in LAB fails to get it too (it uses EnterpriseLibraryContainer.Current.GetInstance<LogWriter>() internally);

Any suggestions?


What do you actually DO with the configuration once you're done with it? Have you put it into a configuration source and fed it to a container or to Entlib?

Your configuration looks ok at first glance, but that just builds the object graph. You need something like this afterwards:

  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)

That should do it. If it doesn't, please post a more detailed example and I can probably debug it for you.

UPDATE

The following example works just fine for me, I just tried it:

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

The only different I can see is I'm using a TextFormatter instead of whatever your StandardFormatter is. Try this and see if it works - if not, then there's something else missing.


I just got a similar error. It was because of an incorrect configuration. I used FormatWithSharedFormatter, but without defining the shared formatter.

I got no complaints during:

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

or:

EnterpriseLibraryContainer.Current
    = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

All I got was that the logger wasn't found when trying to retrieve it from Unity. Removing the FormatWithSharedFormatter configuration made everything work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜