Serilog,在运行时更改特定命名空间的日志级别(> MinimumLevel)

Serilog, Change the loglevel at runtime for a specific namespace (> MinimumLevel)

这是我的默认 Serilog 配置

SeriLogLevelSwitch.MinimumLevel = LogEventLevel.Information;
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.ControlledBy(SeriLogLevelSwitch)
    .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
    .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
    .Enrich.FromLogContext()
     ....

当默认为信息时,如何在运行时将特定名称空间的日志级别更改为调试?

您可以将 环境变量 与另一个 MinimumLevel.Override 配对使用,以便在运行时将特定命名空间的日志级别更改为 Debug,如下所示:

using System;

...

SeriLogLevelSwitch.MinimumLevel = LogEventLevel.Information;
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.ControlledBy(SeriLogLevelSwitch)
    .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
    .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
    .MinimumLevel.Override(Environment.GetEnvironmentVariable("SPECIFIC_NAMESPACE"), LogEventLevel.Debug)
    .Enrich.FromLogContext()
     ....

然后,确保您的应用程序在运行时可以访问环境变量 SPECIFIC_NAMESPACE。请注意 "namespace" 与 "source context prefix"

同义

Environment.GetEnvironmentVariable

您的每个 MinimumLevel.Override 都可以有自己的 LoggingLevelSwitch,这样您就可以在 运行 时控制每个特定覆盖的日志级别。

为您打算在应用程序运行时修改的每个覆盖创建单独的 LoggingLevelSwitch,并将这些实例存储在您可以从应用程序的其他部分访问的地方,这将允许您更改这些 LoggingLevelSwitch(es) 的 MinimumLevel

例如

public class LoggingLevelSwitches
{
    // Logging level switch that will be used for the "Microsoft" namespace
    public static readonly LoggingLevelSwitch MicrosoftLevelSwitch
        = new LoggingLevelSwitch(LogEventLevel.Warning);

    // Logging level switch that will be used for the "Microsoft.Hosting.Lifetime" namespace
    public static readonly LoggingLevelSwitch MicrosoftHostingLifetimeLevelSwitch
        = new LoggingLevelSwitch(LogEventLevel.Information);
}

配置您的 Serilog 日志记录管道以使用这些 LoggingLevelSwitch 个实例:

static void Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Override("Microsoft", LoggingLevelSwitches.MicrosoftLevelSwitch)
        .MinimumLevel.Override("Microsoft.Hosting.Lifetime",
            LoggingLevelSwitches.MicrosoftHostingLifetimeLevelSwitch)
        .Enrich.FromLogContext()
        .CreateLogger();

    // ...
}

然后在您的应用程序的某处,例如,在处理您的应用程序配置的代码中,可以在 运行 时更改,将 LoggingLevelSwitch 个实例更新为新的 LogEventLevel 你想要的:

public class AppSettings
{
    void ChangeLoggingEventLevel()
    {
        LoggingLevelSwitches.MicrosoftHostingLifetimeLevelSwitch
            .MinimumLevel = LogEventLevel.Error;

        LoggingLevelSwitches.MicrosoftHostingLifetimeLevelSwitch
            .MinimumLevel = LogEventLevel.Warning;

        // ...
    }
}

如您所见,LogEventLevelLoggingLevelSwitch 实例控制,因此由您决定在应用程序中的何处(以及如何)修改这些实例,以影响日志管道。

上面的示例我假设您的应用程序中有一个屏幕(或 API),用户可以配置日志记录级别。

如果你没有,那么另一种方法是让后台线程定期检查配置文件、环境变量或查询数据库等,以确定这些日志记录级别应该是什么。

如果您使用的是 .NET Core 主机,则可以使用 Options pattern 它可以为您处理配置刷新,并允许您在配置更改时执行代码(您的位置) d 更改您拥有的 LoggingLevelSwitch(es) 的 MinimumLevel