配置应用程序Insights连接字符串的最佳方法是什么?

发布于 2025-02-10 22:43:52 字数 985 浏览 1 评论 0原文

我有一个ASP.NET Core 6 Web API应用程序。

我通过Visual Studio配置了应用程序的洞察力,添加了在这里。我还配置了本地应用程序文件以在本地存储连接字符串。

现在,我注意到在program.cs中自动生成的代码将变量作为一个参数:

builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);

我想知道 - 此参数真的需要吗?

当我阅读在这里

...呼叫服务。 从 microsoft.extensions.configuration.configuration.configuration。 无需明确提供IconFiguration。

如果我在本地环境中删除它,它可以正常运行,并且仍将日志发送到应用程序。见解。

所以2个问题:

  1. 为什么自动化代码包括此参数?
  2. 如果我删除参数 - 当应用程序在测试或生产中部署时会引起任何问题吗?

I have an Asp.net core 6 Web Api application.

I configured Application Insights through Visual Studio adding a service dependency as described here. I also configured local AppSecrets file to store the Connection string locally.

Now I notice that the code which was automatically generated in Program.cs takes a variable as a parameter:

builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);

I am wondering - is this parameter really needed?

As I read here:

... calling services.AddApplicationInsightsTelemetry() automatically
reads the connection string from
Microsoft.Extensions.Configuration.IConfiguration of the application.
There's no need to explicitly provide the IConfiguration.

If I remove it in my local environment, it works just fine and still sends logs to App. Insights.

So 2 questions:

  1. Why does the autogenerated code include this parameter?
  2. If I remove the parameter - will it cause any problems when the app gets deployed on test or production?

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

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

发布评论

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

评论(1

同尘 2025-02-17 22:43:52

因此,官方文档对所有这一切都是模糊的,一些句子会增加太大的清晰度,所以让我尝试并协助。

为什么自动化代码包括此参数?

截至VS2022,当集成AppInsights SDK时,applicationSights_connection_string_string不再添加为参数,这将是代码的行:

builder.services.addapplication.addapplicationinsightSightStsightSteLemetry()

根据您链接的文档(在这里)建议是正确的。 microsoft.extensions.configuration.iconfiguration如果使用了正确的命名法,将自动读取连接字符串。例如,如果您已经在Azure中配置了AppinSights,则在configuration下为应用程序服务看到以下值:

applicationsights_connection_connection_string

该值将在每个部署到每个时都会自动读取环境;不需要在appSettings.json的主要源中本地添加其他连接字符串信息。只要这些连接字符串存在于Azure(配置AppInsights时,它会在Azure中自动创建,或者您可以通过ARM模板使用DevOps等配置DevOps等,它将允许AppinSights在本地工作和

部署=“ https://github.com/microsoft/applicationinsights-dotnet/issues/1882” rel =“ nofollow noreferrer”> link 很好地解释了他们如何在调用code> AddagplicationSightStsTemtSteMetry( )

如果我删除了参数 - 应用程序时会引起任何问题
部署在测试或生产中?

因此,这是模糊的很酷的部分 - 删除该参数不会导致其部署时其破裂,将在多个环境中工作,因为每个不同环境都将使用相同的密钥名称拾取值读取的值。因此,除非您明确具有在AppSettings.json中覆盖的价值builder.services.addapplicationinsightSteleMetry();的单行将在AppSettings.json中寻找applicationsights_connection_string_string

So the official documentation is vague on all this and a few sentences would add so much clarity, so let me try and assist.

Why does the autogenerated code include this parameter?

As of VS2022, when integrating the AppInsights SDK, that APPLICATIONINSIGHTS_CONNECTION_STRING is no longer added as a parameter and this will be the line of code:

builder.Services.AddApplicationInsightsTelemetry();

Per that documentation you linked (here) the advice is correct. The Microsoft.Extensions.Configuration.IConfiguration automatically will read the connection string if the correct nomenclature is used. For example, if you have already configured AppInsights in Azure, you will see under Configuration for the App Service the following value:

APPLICATIONINSIGHTS_CONNECTION_STRING

That value will be read automatically once deployed to each environment; no additional connection string information needs to be added locally in the main source to appsettings.json. It will allow AppInsights to work locally and deployed, so long as those connection strings exist in Azure (which get automatically created for you in Azure when configuring AppInsights, or you can configure using DevOps via ARM templates, etc.)

This GitHub link explains the behavior well of how they read the configuration provider upon calling AddApplicationInsightsTelemetry()

If I remove the parameter - will it cause any problems when the app
gets deployed on test or production?

So this is the cool part that's vague - removing that parameter will not cause it to break when deployed, and will work for multiple environments as the value read will be picked up with the same key name per different environments. Therefore unless you explicitly have a value to override in appsettings.json in your own source, you can strictly leave all the connection string values in Azure for 1..n environments, and the single line of builder.Services.AddApplicationInsightsTelemetry(); will look for APPLICATIONINSIGHTS_CONNECTION_STRING in appsettings.json per each deployed environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文