MAT*_*HEW 5 c# asp.net nlog asp.net-core
目标:动态选择我想要详细记录的 HTTP 请求(不同的日志级别)。
概述:我有一个 ASP.Net core 2.1 Web 服务器正在运行,并且一旦投入生产,如果我需要调试问题,我希望能够更改日志级别。我找到了如何全局更改日志级别;但是,更改日志级别是持久的......也就是每次调用我的控制器后都不会重置。
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
this.Logger.LogTrace("This should NOT get logged");
SetMinLogLevel(LogLevel.Trace);
this.Logger.LogTrace("This should be logged");
return new string[] { "value1", "value2" };
}
public static void SetMinLogLevel(LogLevel NewLogLevel)
{
foreach (var rule in LogManager.Configuration.LoggingRules)
{
rule.EnableLoggingForLevel(NewLogLevel);
}
//Call to update existing Loggers created with GetLogger() or
//GetCurrentClassLogger()
LogManager.ReconfigExistingLoggers();
}
Run Code Online (Sandbox Code Playgroud)
我希望请求者能够在他们的 HTTP 请求(标头或 cookie)中设置一个标志,以便为每个请求启用更详细的日志记录级别。这样我就不会用来自请求者的详细日志淹没我的日志。
问题:如何动态设置每个记录器实例的日志级别?(我相信这是正确的措辞)
我目前正在使用 NLog 包 4.5。
NLog 4.6.7 允许您在loggingrules过滤器中使用布局minLevel/maxLevel
您可以拥有一个具有默认日志级别的 NLog-Config-Variable,然后在您的 Web 应用程序上创建一个隐藏方法来修改 NLog-Config-Variable 并调用ReconfigExistingLoggers().
然后设置一个计时器,在 30 秒后将该 NLog-Config-Variable 恢复为其原始值。并且还打电话ReconfigExistingLoggers()。
另请参阅: https: //github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
另请参阅:https ://github.com/NLog/NLog/wiki/Environment-specific-NLog-Logging-Configuration
| 归档时间: |
|
| 查看次数: |
982 次 |
| 最近记录: |