gpt4 book ai didi

azure - 如何在不依赖注入(inject)的情况下配置 TelemetryConfiguration?

转载 作者:行者123 更新时间:2023-12-03 06:32:02 25 4
gpt4 key购买 nike

我在 Web 作业中使用 Application Insights。目前我不使用依赖注入(inject)。我需要配置 TelemetryChannel 以便能够在退出 webjob 之前刷新提供程序。是否可以在不使用 DI 的情况下将 TelemetryConfiguration 配置/设置/注入(inject)到 App Insights 的日志记录提供程序中?

我使用以下语句来添加日志记录提供程序。

    builder.AddApplicationInsightsWebJobs(o => o.ConnectionString = connStr).SetMinimumLevel(LogLevel.Information);

最佳答案

configure TelemetryConfiguration without dependency injection?

要在没有DI的情况下配置遥测,我们需要将Instrumentation Key或ConnectionString传递给TelemetryConfiguration

我尝试从 appsettings.json 设置 key

安装 NuGet 包 Microsoft.ApplicationInsights.Web

enter image description here

我的 appsettings.json 文件

{
"Logging": {
"ApplicationInsights": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Error"
}
},
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},

"AllowedHosts": "*",
"ApplicationInsights": {
"ConnectionString": "****",
"Instrumentationkey": "****"
}
}

谢谢@rogerpence.dev获取代码片段。

我尝试遵循给定的代码,但收到以下错误。

 var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();

enter image description here

目前,我已在 Program.cs 文件本身中设置了 key 。

Is it possible to configure/set/inject the TelemetryConfiguration into the logging provider for App Insights without DI?

Visual Studio 中,右键单击解决方案根文件夹 => 添加 => 连接服务

enter image description here

  • 添加 Application Insights 服务依赖项

enter image description here

我的.csproj文件

<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.Web" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Settings.job">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

我们有MSDoc其中解释了在不依赖注入(inject)的情况下配置遥测。

Program.cs

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DependencyCollector;
using Microsoft.ApplicationInsights.Extensibility;
using System.Net.Http;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.ConnectionString = "Copy the Connectionstring from AppInsights";
//configuration.InstrumentationKey = "****";
var telemetryClient = new TelemetryClient(configuration);
telemetryClient.TrackTrace("Hello World!");
telemetryClient.TrackTrace("This is the Telemetry Logs!");
telemetryClient.Flush();
}
}
}

输出:

enter image description here

关于azure - 如何在不依赖注入(inject)的情况下配置 TelemetryConfiguration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75184184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com