.NET 6带应用程序见解的Serilog仪器密钥未被拾取

发布于 2025-02-06 09:25:06 字数 2980 浏览 4 评论 0原文

我们有一个.NET6 Web API和Worker项目,我们试图利用Serilog登录应用程序见解。

我们在公共类库中存在一个扩展方法,该方法在两个解决方案中都使用:

    /// <summary>
    /// Extension IHostBuilder method for configuring logging in projects.
    /// </summary>
    /// <param name="hostBuilder">IHostbuilder.</param>
    /// <returns>IHostBuilder.</returns>
    public static IHostBuilder ConfigureLogging(this IHostBuilder hostBuilder)
    {
        hostBuilder.UseSerilog((hostingContext, loggerConfiguration) =>
        {
            loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
    });

        return hostBuilder;
        }

我们在program.cs文件中使用此方法。Carvar

builder = webapplication.createbuilder(args); builder.host.configurelogging();

以下软件包安装在公共库和API库

&lt; itemGroup&gt;&lt; packagereference include =“ serilog..aspnetcore”版本=“ 5.0” /&gt; &lt; packageReference包括=“ serilog.settings.configuration”版本=“ 3.3.0” /&gt; &lt; packageReference包括=“ serilog.sinks.applicationinsights”版本=“ 3.1.0” /&gt; &lt;/itemGroup&gt;

上面的软件包也安装在API项目中。

我们将其放置在Serilog块中时会拾取仪器密钥。但是,根据他们的指导,这是在此处的指导: https://github.com/serilog-contrib/serilog-serilog-sinks-applicationsights#configuring-with-readfiguring-with-readfromconfromconfiguration - 钥匙,但它是积极劝阻的:“

我们在AppSettings中具有以下配置。API项目中的JSON作为一个示例,当我们在内部引用仪器密钥内部的仪器时起作用:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.ApplicationInsights"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
           "instrumentationkey: "<key goes here>", 
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ],
    "Properties": {
      "Application": "Solution.Api"
    }
  }
}

我们还可以通过添加环境变量来通过使用环境变量来使其正常工作进入启动集

"Solution.Api": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7194;http://localhost:5194",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "APPINSIGHTS_INSTRUMENTATIONKEY": "<Insert instrumentation key>"
      }
    },

。 =“ https://github.com/serilog-contrib/serilog-sinks-applicationinsights/issues/176” rel =“ nofollow noreferrer”> https://github.com/serilog-conilog-contrib/serilog-contrib/serilog-sink-sinks-applicationsightsights/issigns/issues/issues /176 想知道是否有人知道问题是我们缺少的?

We have a .net6 web API and worker project that we are trying to leverage Serilog to log to app insights.

We have an extension method existing in a common class library that is used in both solutions:

    /// <summary>
    /// Extension IHostBuilder method for configuring logging in projects.
    /// </summary>
    /// <param name="hostBuilder">IHostbuilder.</param>
    /// <returns>IHostBuilder.</returns>
    public static IHostBuilder ConfigureLogging(this IHostBuilder hostBuilder)
    {
        hostBuilder.UseSerilog((hostingContext, loggerConfiguration) =>
        {
            loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
    });

        return hostBuilder;
        }

We use this in the program.cs file

var builder = WebApplication.CreateBuilder(args); builder.Host.ConfigureLogging();

The following packages are installed in the common library and in the api library

<ItemGroup><PackageReference Include="Serilog.AspNetCore" Version="5.0" /> <PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" /> <PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.1.0" /> </ItemGroup>

The above packages are also installed in the API project.

We have it picking up the instrumentation key when placed inside the serilog block.. however this is discouraged as per their guidance here:https://github.com/serilog-contrib/serilog-sinks-applicationinsights#configuring-with-readfromconfiguration - "As mentioned above you can also pass an instrumentation key but it's actively discouraged"

We have the following configuration in appsettings.json in the API project as an example and it works when we reference the instrumentation key inside as shown below:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.ApplicationInsights"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
           "instrumentationkey: "<key goes here>", 
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ],
    "Properties": {
      "Application": "Solution.Api"
    }
  }
}

We can also get it working through using environment variables, by adding the following into launchsettings.json:

"Solution.Api": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7194;http://localhost:5194",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "APPINSIGHTS_INSTRUMENTATIONKEY": "<Insert instrumentation key>"
      }
    },

Ultimately, neither of these approaches seem correct and we want to have the instrumentation key in appSettings decoupled from serilog config as per best practice It seems this is a known issue and may be to do with .net6 https://github.com/serilog-contrib/serilog-sinks-applicationinsights/issues/176 Wondering if anyone knows what the issue is/we are missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2025-02-13 09:25:06

我对这个问题的理解是,与应用程序构建器管道的一部分相比,应用程序洞察力向标准服务的转移。

Serilog是App Builder管道的一部分,然后尝试在构建服务提供商之前从AppSettings.json配置汇,因此(当前)不知道如何在配置对象中查找仪器键t拥有。

我们围绕与GitHub问题的链接所阐述的问题的工作176是使用Fluent API配置应用程序Insights insights inter-aption,

loggerConfiguration.ReadFrom.Configuration(context.Configuration)
                            .WriteTo.ApplicationInsights(
                                   provider.GetRequiredService<TelemetryConfiguration>(),
                                   TelemetryConverter.Traces);

这使我们可以使用仪表键(或连接字符串),因为它的意思是根据应用Insights文档使用。

这确实限制了配置驱动的控制跟踪数据级别的控制,该控制级别通过Serilog将其通过Serilog到提供商,但是使用标准记录提供商配置允许 在这些之后应用到跟踪日志已经通过Serilog。由于这是直接应用于记录提供商的serilog过滤器,因此只能用于添加限制。它不能用于添加异常。

除这种方法外,您已经详细介绍了我认为是工作设置的其他两个选项:

  1. 在Serilog sink中添加仪器键
  2. 使用仪器键用作环境变量

My understanding of this issue is that it is around the move of application insights to a standard service as opposed to part of the app builder pipeline.

Serilog, as part of the app builder pipeline, is then trying to configure the sink from appsettings.json before the service provider is built, and therefore doesn't (currently) know how to look for the instrumentation key in a config object it doesn't own.

Our work around of the issue you've spelled out with a link to the GitHub issue 176 is to configure the Application Insights sink using the fluent api

loggerConfiguration.ReadFrom.Configuration(context.Configuration)
                            .WriteTo.ApplicationInsights(
                                   provider.GetRequiredService<TelemetryConfiguration>(),
                                   TelemetryConverter.Traces);

This work-around allows us to use the instrumentation key (or connection string) as it is meant to be used according to the App Insights documentation.

This does limit configuration driven control over levels of trace data that makes it through Serilog to the provider, but using standard logging provider configuration allows additional restriction to be applied to traces after those logs have passed through Serilog. As this is an after-Serilog filter applied directly to the logging provider, it can only be used to add restriction. It cannot be used to add exceptions.

Apart from this approach, you have already detailed the other two options I've seen as working setup:

  1. include the instrumentation key in the Serilog sink config
  2. use instrumentation key as an environment variable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文