首页 > 其他分享 >serilog 动态更新日志级别

serilog 动态更新日志级别

时间:2022-11-10 18:25:40浏览次数:61  
标签:serilog loggerConfiguration LoggerConfiguration new 日志 Configuration 级别 public

使用这个库,更新配置文件,就可以动态更新 日志输出级别。

new LoggerConfiguration().ReadFrom.Configuration(hostingContext.Configuration)

这个Configuration 定义在这儿

return settingConfiguration.Settings(
                new ConfigurationReader(
                    configuration.GetSection(sectionName),
                    assemblyFinder,
                    configuration));

进到这个 reader 里面,有 实现接口的方法:这个 Configure method 会在 上面的方法直接调用。

 1 public void Configure(LoggerConfiguration loggerConfiguration)
 2         {
 3             ProcessLevelSwitchDeclarations();
 4             ProcessFilterSwitchDeclarations();
 5 
 6             ApplyMinimumLevel(loggerConfiguration);
 7             ApplyEnrichment(loggerConfiguration);
 8             ApplyFilters(loggerConfiguration);
 9             ApplyDestructuring(loggerConfiguration);
10             ApplySinks(loggerConfiguration);
11             ApplyAuditSinks(loggerConfiguration);
12         }

其中有ApplyMinimumLevel, 这是设置日志级别的地方:

这下面会调用 ChangeToken.OnChange... 

that is when IConfiguration changed, a delegate will be trigger.

and this will change the LoggerConfiguration's MinimumLevel.ControlledBy(levelSwitch)

so below code also can change the level dynamicly:

public static LoggingLevelSwitch loggingLevelSwitch = new LoggingLevelSwitch();
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging((hostingContext, loggingBuilder) =>
                {

                    Log.Logger = new LoggerConfiguration()
                        .MinimumLevel.ControlledBy(loggingLevelSwitch)
                        .WriteTo.Console()
                        .CreateBootstrapLogger();
                    
                    loggingBuilder.ClearProviders();
                    loggingBuilder.AddSerilog();
                })
                .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

we can change the level at any time without the application restarting.

 

标签:serilog,loggerConfiguration,LoggerConfiguration,new,日志,Configuration,级别,public
From: https://www.cnblogs.com/qgbo/p/16877966.html

相关文章

  • SpringBoot自定义日志Starter(二十五)
    即使有一天,我放弃了自己的身体,也请你,不要放弃我,我亲爱的灵魂.上一章简单介绍了SpringBoot自定义Starter(二十四),如果没有看过,​​请观看上一章​​一.AOP实现日志功能......
  • logback-spring.xml文件详解,日志优化
    依赖<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.3</version></dependency><dependency><groupId......
  • Java输出SSL握手日志和查看cacerts路径
    在JAVA启动时添加下面的VM参数就可以启动握手日志了!!!-Djavax.net.debug=all另外,在debug日志中,有一个trustStoreis关键字,根据这个可以找到使用的是哪个truststor......
  • 利用panda实现日志可视化分析的脚本
    1.准备慢日志的csv文件importpandasaspdimportmatplotlib.pyplotasplt#选取耗时大于7000的日志#awk-F'耗时:''{if(int(substr($2,0,length($2)-2))>7......
  • Linux查看大文件日志
    Linux查看大日志文件1、使用less命令 lessfilename但是使用上述命令的坏处是,默认打开的位置在第一行,并且当切换到实时滚动模式(按 F ,实现效果类似tail-f效......
  • awk分析nginx日志
    nginx日志字段说明127.0.0.1--[31/Aug/2018:16:11:16+0800]"GET/50x.htmlHTTP/1.1"200537"-""curl/7.29.0"访问ip、访问时间、请求方式、请求url、响应状态码......
  • Oracle11g redo log 创建、添加、删除(重做日志组,重做日志文件)
    selectgroup#,sequence#,members,bytes,status,archivedfromv$log;selectgroup#,status,type,memberfromv$logfile;添加redo组alterdatabaseaddlogfile(......
  • tempdb日志文件暴增分析
    背景某医院信息科接到CIS系统磁盘空间不足告警,通过排查发现tempdb的日志文件暴增,已经涨到了130G左右,并且还在持续增长中。需要我们紧急排查原因。现象登陆到服务器里,确......
  • 018.MyBatis日志管理
    1.SLF4j与Logback   2.自定义使用logback日志2.1 pom.xml<dependency><groupId>ch.qos.logback</groupId><artifactId>log......
  • spdlog日志库源码:sinks系列类
    目录sinks系列类简介特点sinks继承体系sink类sink类声明日志等级阈值sink子类null_sink类模板base_sink类模板basic_file_sink类模板文件工具类file_helperdaily_file_sink......