我正在使用 EF 5 并尝试使用 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 方法,我确实会收到写入日志的条目。难道我做错了什么?