Fluent API for unit tests error message

Topics: Logging Application Block
Jun 24, 2010 at 2:07 AM

I'm trying to use the fluent api to configure the logging application block for our unit tests, using the following code:

            IConfigurationSource configurationSource = new SystemConfigurationSource();
            if (configurationSource.GetSection(LoggingSettings.SectionName) != null)
                return EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();                

            const string textFormatter = "Timestamp: {timestamp}\nMessage: {message}\nCategory: {category}\nPriority: {priority}\nEventId: {eventid}\nSeverity: {severity}\nTitle:{title}\nMachine: {machine}\nApplication Domain: {appDomain}\nProcess Id: {processId}\nProcess Name: {processName}\nWin32 Thread Id: {win32ThreadId}\nThread Name: {threadName}\nExtended Properties: {dictionary({key} - {value})}";

            var configurationBuilder = new ConfigurationSourceBuilder();
                .SendTo.EventLog("Formatted EventLog TraceListener")
                .FormatWith(new FormatterBuilder()
                .TextFormatterNamed("Text Formatter").UsingTemplate(textFormatter))
            var configSource = new DictionaryConfigurationSource();
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

            return EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();                

But I get the error when trying to resolve the LogWriter:

InvalidOperationException - The current type, Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.ILogFormatter, is an interface and cannot be constructed. Are you missing a type mapping?

What am I missing from the configuration? I just want a simple as possible configuration that sends everything to the event log for when no settings are found.

Jun 24, 2010 at 7:10 AM


Unfortunately using your code snippet above I'm unable to reproduce your problem and it is working as expected on my end. Could you send to us your sample app for this so we can able to look closer. Thanks.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.

Jun 24, 2010 at 7:59 AM

Thanks, for the offer, but I just got it working, I started from the example in the chm, and slowly added what I wanted piece by piece, making sure each new step worked.

And I have it doing exactly what I want now.




Jun 24, 2010 at 8:01 AM

Sounds good.. glad to hear about it :-)

Sep 13, 2012 at 2:41 AM

i am using the following code to configure logging. i have to attach two listeners: msmq listener and event log listener to the category: "General". but iam getting the below error while creating the log writer:

 The current type, Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.ILogFormatter, is an interface and cannot be constructed. Are you missing a type mapping?.

below is the code:

 string s= @"DIRECT=OS:mycomputername\private$\company.integration.common.logging.nontransactional";                    string str = "FormatName:" + "" + s;                    var builder = new ConfigurationSourceBuilder();
                    builder.ConfigureLogging()                            .WithOptions                                .DoNotRevertImpersonation()                            .LogToCategoryNamed("General")                                .SendTo.Msmq("MSMQ Trace Listener")                                .FormatWith(new FormatterBuilder().BinaryFormatterNamed("BinaryFormatter"))                                .UseQueue(str)                                .WithTransactionType(MessageQueueTransactionType.None)                                .Prioritize(MessagePriority.AboveNormal)                                .SendTo.EventLog("Event Log Listener").                                FormatWithSharedFormatter("Text Formatter")                                .SendTo.EventLog("Formatted EventLog TraceListener")                                .FormatWithSharedFormatter("Text Formatter")                                .ToLog("Application")                                .LogToCategoryNamed("EventViewLogging")                                .WithOptions.SetAsDefaultCategory()                                .SendTo.SharedListenerNamed("Formatted EventLog TraceListener");                                //UsingEventLogSource("PETRO_LOG")                                //.ToLog("Petrodex")                                ;
                    var configSource = new DictionaryConfigurationSource();                    builder.UpdateConfigurationWithReplace(configSource);                    EnterpriseLibraryContainer.Current                        = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);                   var writer= EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();


Sep 13, 2012 at 8:26 AM

The issue is the use of FormatWithSharedFormatter("TextFormatter") but that Formatter is never defined.  Change the first  FormatWithSharedFormatter("TextFormatter") to FormatWith(new FormatterBuilder().TextFormatterNamed("Text Formatter")                      .UsingTemplate("Message: {message}..."))

Randy Levy
Enterprise Library support engineer