2012-01-13 17 views
3

EntLib 5 Logging Application Blockを簡略化するロギングDLLを構築しています。私はConfigurationSourceBuilderを使って私のアプリケーションのロギングを設定しています。私は現在これを持っています:Fluent ConfigurationSourceBuilderが原因でInvalidOperationExceptionが発生する

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

プログラムはビルドされ、私はメインのアプリケーションでそれを参照します。 IoCのよう

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

ただ、この問題場合には、メインアプリケーションが使用しているユニティ:それは、このエラーを吹く構成を設定するために行くとき。 UnityでDLLを解決する必要はありますか?

答えて

3

実際にフォーマッタを作成していないというのは私の目に見えます。既存のフォーマッタを、文で.FormatWithSharedFormatter("Text Formatter")という名前で参照しました。

あなたがFormatterBuilderを使用してフォーマッタを定義する場合、それは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"); 
+1

うわーとても簡単!多くのTuzoありがとう –

関連する問題