gpt4 book ai didi

c# - Fluent ConfigurationSourceBuilder 导致 InvalidOperationException

转载 作者:行者123 更新时间:2023-11-30 16:26:23 26 4
gpt4 key购买 nike

我正在构建一个日志记录 DLL,它将简化 EntLib 5 日志记录应用程序 block 。我正在使用 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");

关于c# - Fluent ConfigurationSourceBuilder 导致 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8853132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com