我正在构建一个日志DLL,它将简化EntLib 5 Logging Application 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吗?
答案 0 :(得分:3)
在我看来,问题是你还没有真正创建过Formatter。您已使用语句.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");