Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent ConfigurationSourceBuilder Causes InvalidOperationException

I am building a logging DLL that will simplify EntLib 5 Logging Application Block. I am using ConfigurationSourceBuilder to configure the logging for my application. I currently have this:

var configBuilder = new ConfigurationSourceBuilder();

        configBuilder.ConfigureLogging().WithOptions
            .DoNotRevertImpersonation()
            .LogToCategoryNamed("EventLog")
                .WithOptions.SetAsDefaultCategory()
                .SendTo.EventLog("Event Log Listener")
                .FormatWithSharedFormatter("Text Formatter")
                .ToLog("Application")
            .LogToCategoryNamed("Email")
                .SendTo.Email("Email Trace Listener")
                .To(ToEmail)
                .From(fromEmail)
                .WithSubjectStart("Error:")
                .UsingSmtpServer(SmtpServer)
                .UsingSmtpServerPort(SmtpServerPort)
                .Unauthenticated()
                .FormatWithSharedFormatter("Text Formatter")
            .LogToCategoryNamed("LogFile")
                .SendTo.FlatFile("Flat File Trace Listener")
                .ToFile(logFileName)
                .WithHeader("------------------------------")
                .WithFooter("------------------------------")
                .FormatWithSharedFormatter("Text Formatter");

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

The program will build and I will reference it in the main app. When it goes to set up the configuration it blows up with this error:

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

Just in case this matters, the main app is using Unity as an IoC. Do I need to resolve the DLL with Unity?

like image 298
Aarron H Avatar asked Sep 01 '26 13:09

Aarron H


1 Answers

It looks to me like the issue is that you haven't actually created a Formatter. You've referenced an existing formatter by name with the statement .FormatWithSharedFormatter("Text Formatter").

If you define a formatter using FormatterBuilder then it should be OK:

var configBuilder = new ConfigurationSourceBuilder();

configBuilder.ConfigureLogging().WithOptions
.DoNotRevertImpersonation()
.LogToCategoryNamed("EventLog")
    .WithOptions.SetAsDefaultCategory()
    .SendTo.EventLog("Event Log Listener")
    .FormatWith(
        new FormatterBuilder()
            .TextFormatterNamed("Text Formatter")
            .UsingTemplate("Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}")
        )
    .ToLog("Application")
.LogToCategoryNamed("Email")
    .SendTo.Email("Email Trace Listener")
    .To(ToEmail)
    .From(fromEmail)
    .WithSubjectStart("Error:")
    .UsingSmtpServer(SmtpServer)
    .UsingSmtpServerPort(SmtpServerPort)
    .Unauthenticated()
    .FormatWithSharedFormatter("Text Formatter")
.LogToCategoryNamed("LogFile")
    .SendTo.FlatFile("Flat File Trace Listener")
    .ToFile(logFileName)
    .WithHeader("------------------------------")
    .WithFooter("------------------------------")
    .FormatWithSharedFormatter("Text Formatter");
like image 90
Randy supports Monica Avatar answered Sep 03 '26 03:09

Randy supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!