私はEF5を使用しており、ShouldLogメソッドを使用して、実際にログを記録する前にLogEntryがログに記録されるかどうかを判断しようとしています。私の問題は、特定のレベルを除外するためのフィルターが設定されている場合でも、ShouldLogが常にtrueを返すことです。フィルタは機能し、エントリはログに記録されませんが、ShouldLogは機能していないようです。
私はロガーを次のように構成しています:
internal static void ConfigureLogging(SourceLevels logLevel)
{
var builder = new ConfigurationSourceBuilder();
builder.ConfigureLogging()
.LogToCategoryNamed("General")
.WithOptions.SetAsDefaultCategory()
.SendTo.FlatFile("Main log file")
.FormatWith(
new FormatterBuilder()
.TextFormatterNamed("Text Formatter")
.UsingTemplate("{timestamp(local:MM/dd/yyyy HH:mm:ss.fff)} [{severity}] {message}"))
.Filter(logLevel) //Setting the source level filter
.ToFile("log.txt");
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
}
そして、次のようにテストします。
ConfigureLogging(SourceLevels.Warning); //Do not allow Information level
var logEntry = new LogEntry { Message = "test", Severity = TraceEventType.Information };
var shouldLog = Logger.Writer.ShouldLog(logEntry);
Logger.Writer.Write(logEntry);
このコードを実行した後、shouldLog変数はtrueですが、ログエントリは書き込まれません。SourceLevels.InformationをConfigureLoggingメソッドに渡すと、代わりにログにエントリが書き込まれます。私は何か間違ったことをしていますか?