亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 appsettings.json 覆蓋 serilog 設置

如何使用 appsettings.json 覆蓋 serilog 設置

C#
www說 2022-12-24 10:12:57
我有一個擴展方法來配置我的記錄器: public static class Extensions {     public static IWebHostBuilder UseLogging(this IWebHostBuilder webHostBuilder) =>         webHostBuilder.UseSerilog((context, loggerConfiguration) =>         {             var logLevel = context.Configuration.GetValue<string>("Serilog:MinimumLevel");             if (!Enum.TryParse<LogEventLevel>(logLevel, true, out var level))             {                 level = LogEventLevel.Information;             }             loggerConfiguration.Enrich                 .FromLogContext()                 .MinimumLevel.Is(level);             loggerConfiguration                 .ReadFrom.Configuration(context.Configuration)                 .WriteTo.Console(                     theme: AnsiConsoleTheme.Code,                     outputTemplate: "[{Timestamp:yy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}");         }); }我添加了這一行.ReadFrom.Configuration(context.Configuration)并期望來自 appsettings.json 的所有設置都將覆蓋當前配置。我的意思是,如果我在 appsettings.json 中指定其他 outputTemplate,那么它將覆蓋現有的。    "Serilog": {        "WriteTo": [            {                "Name": "Console",                "Args": {                    "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"                }            }        ]    }但它不起作用?,F在日志消息是重復的,一個是代碼格式的,另一個是配置格式的。如何在運行時覆蓋 appsettings.json 中的一些設置?[03:59:09 INF] 事件處理器服務正在啟動。[19-04-19 00:59:09 INF] 事件處理器服務正在啟動。我有在服務中使用的擴展方法,但有時我需要覆蓋 appsettings.json(或環境變量)中的某些設置,我該怎么做?因為當前的解決方案不起作用并添加了第二個記錄器
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

您基本上必須檢查配置是否已經指定了控制臺記錄器。存儲接收器的字段是私有的。所以你必須使用類似的東西來對配置做出反應。當您想要覆蓋記錄器上的某些屬性時,您還需要使用反射來訪問這些字段。下面的代碼適用于我的簡單測試應用程序。


    public static class Extensions

    {

        public static IWebHostBuilder UseLogging(this IWebHostBuilder webHostBuilder) =>

            webHostBuilder.UseSerilog((context, loggerConfiguration) =>

            {

                var logLevel = context.Configuration.GetValue<string>("Serilog:MinimumLevel");

                if (!Enum.TryParse<LogEventLevel>(logLevel, true, out var level))

                {

                    level = LogEventLevel.Information;

                }


                loggerConfiguration.Enrich

                    .FromLogContext()

                    .MinimumLevel.Is(level);


                loggerConfiguration

                    .ReadFrom.Configuration(context.Configuration);


                // Get the field that holds the sinks.

                var sinks = loggerConfiguration.GetType()

                    .GetField("_logEventSinks", BindingFlags.Instance | BindingFlags.NonPublic)

                    .GetValue(loggerConfiguration) as List<ILogEventSink>;


                // Get the sink type for reusage.

                var sinkType = typeof(AnsiConsoleTheme).Assembly.GetType("Serilog.Sinks.SystemConsole.ConsoleSink");


                // Find the first sink of the right type.

                var sink = sinks?.FirstOrDefault(s => sinkType == s.GetType());


                // Check if a sink was found.

                if (sink == null)

                {

                    // No sink found add a new one.

                    loggerConfiguration

                        .WriteTo.Console(

                            theme: AnsiConsoleTheme.Code,

                            outputTemplate:

                            "[{Timestamp:yy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}");

                }

                else

                {

                    // Otherwise change the theme.

                    sinkType.GetField("_theme", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(sink, AnsiConsoleTheme.Code);

                }

            });

    }


查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號