c# - 我不想直接输入应用程序洞察的 key 来注册日志

标签 c# azure asp.net-core logging azure-application-insights

我不想在 Program.cs 中键入应用程序洞察的 key ,我可以将其键入到某个配置文件或其他位置吗?这是一个 ASP .Net Core

我想在我的日志的应用程序洞察中包含一个寄存器,但进行了修改。现在我有一个寄存器,但我在 Program.cs 中键入 key ,当我更改环境时遇到“问题”。您知道在 Program.cs 上动态键入此键的方法吗?或者我可以在程序的其他位置执行此声明吗?

这是 Program.cs。它从 Main 开始,在启动 BuildWebHost 后,我​​在其中加载应用程序见解的 key ,这就是我想要更改的内容:

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureLogging(logging =>
        {
                logging.AddApplicationInsights("db0fe38d-c208-8ed7-23e4ef4479bb");

                // Optional: Apply filters to configure LogLevel Trace or above is sent to
                // ApplicationInsights for all categories.
                logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);

                // Additional filtering For category starting in "Microsoft",
                // only Warning or above will be sent to Application Insights.
                logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
        }).Build();

我怎么说,我会避免在程序上键入 key ,并且我想从配置文件中获取此参数或在其他位置键入此声明

最佳答案

由于它是 ASP.NET Core 应用程序,因此您可以使用注入(inject) WebHostBuilderContextConfigureLogging 扩展方法来检索配置:

 public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureLogging((hostingContext, logging) =>
        {
                var appInsightKey =  hostingContext.Configuration["MyAppInsight"];
                logging.AddApplicationInsights(appInsightKey);

                // Optional: Apply filters to configure LogLevel Trace or above is sent to
                // ApplicationInsights for all categories.
                logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);

                // Additional filtering For category starting in "Microsoft",
                // only Warning or above will be sent to Application Insights.
                logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
        }).Build();

关于c# - 我不想直接输入应用程序洞察的 key 来注册日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55352678/

相关文章:

c# - KnockoutMVC 提交按钮不起作用

node.js - 使 *.crt *.pem 证书在 Docker 容器上运行的 Azure 应用服务内可用

MySqlException : Fatal error encountered during command execution . Asp.net Core

sql - 通知客户数据库更改的最佳方法

azure - Orchard Azure 网站错误

c# - 在 Razor 网页上显示静态 .png 图像

c# - HostingEnvironment 不包含 IsHosted 的定义

c# - 单元/集成测试 FTP 访问

c# - 正则表达式/linq 用计数替换连续字符

c# - 在单个 ftp 请求中创建一个包含子目录的目录?