serilog 只记录调试、错误和警告日志,而不记录信息 [英] serilog to log only debug, error and warning logs but not information
问题描述
我有一个配置了用于日志记录的 serilog 的应用程序.我的应用程序中有一个复选框,只有单击该复选框按钮时,它才会记录所有信息日志,否则只记录错误和警告日志.
I have an application with serilog configured for logging. I have a checkbox in my application, only on clicking that checkbox button it should log all information log else only Error and Warning log should be logged.
在调试模式下,如何在取消选中按钮时仅记录调试、错误和警告日志.从 2 天以来我一直在尝试这样做.我已经使用 .MinimumLevel.Debug()
/restrictedToMinimumLevel: LogEventLevel.Debug
尝试了 google/stackoverflow 中建议的所有方法,但它对我不起作用.它也记录信息日志.
In debug mode how to log only Debug, error and warning log when the button is unchecked. I have been trying to do this since 2 days. I have tried all the ways suggested in google/stackoverflow using .MinimumLevel.Debug()
/restrictedToMinimumLevel: LogEventLevel.Debug
it did not work for me. It logs Information log as well.
请帮我解决这个问题.仅记录调试、错误和警告日志.
Please help me on this. To log only Debug,error and warning log.
val
在复选框启用时为真:
val
is true when checkbox enabled:
if (val == "true")
{
_logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(_filepath, restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
}
else if (val == "false")
{
_logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(_filepath, restrictedToMinimumLevel: LogEventLevel.Debug)
.CreateLogger();
}
当 val
为 false 时,即未启用复选框时,应仅记录调试、错误和警告日志.
when val
is false, it.e., when check box is not enabled, it should log only debug,error and warning logs.
推荐答案
您无法使用 MinimumLevel
作为 @barbsan 注释来实现它.
You cannot achieve it using MinimumLevel
as @barbsan comment.
但是您可以使用过滤来处理您的情况.
But you can use filtering for in your case.
过滤器只是对 LogEvent
_logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(_filepath)
.Filter.ByIncludingOnly(isNotInfoLevel)
.CreateLogger();
在下面的函数中,您可以根据需要添加更多条件
And in below function you can add more conditions if needed
private static bool isNotInfoLevel(LogEvent le)
{
return le.Level != LogEventLevel.Information;
}
使用ByExclusion
排除信息层的另一种方法
Anther way by using ByExcluding
to exclude information level
.Filter.ByExcluding((le) => le.Level == LogEventLevel.Information)
这篇关于serilog 只记录调试、错误和警告日志,而不记录信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!