编程语言
首页 > 编程语言> > c#-流利的ConfigurationSourceBuilder导致InvalidOperationException

c#-流利的ConfigurationSourceBuilder导致InvalidOperationException

作者:互联网

我正在构建一个日志记录DLL,它将简化EntLib 5日志记录应用程序块.我正在使用ConfigurationSourceBuilder为我的应用程序配置日志记录.我目前有这个:

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);

该程序将构建,我将在主应用程序中引用它.设置配置时,它会因以下错误而崩溃:

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

万一这很重要,主应用程序将Unity用作IoC.我需要使用Unity解析DLL吗?

解决方法:

在我看来,问题在于您实际上尚未创建格式化程序.您已使用语句.FormatWithSharedFormatter(“ Text Formatter”)引用了现有的格式化程序.

如果使用FormatterBuilder定义格式化程序,则应该可以:

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");

标签:logging,unity-container,enterprise-library,asp-net,c
来源: https://codeday.me/bug/20191201/2084071.html