gpt4 book ai didi

c# - Azure Application Insights 不显示在 Azure 函数中创建的自定义事件

转载 作者:行者123 更新时间:2023-12-02 06:17:15 29 4
gpt4 key购买 nike

我有一个在 Azure 中创建的用 C# 编写的函数应用程序,由 HttpTrigger 触发。

我正在尝试记录几个自定义事件以记录分析性能的有趣时间点。

我所做的是在构造函数中创建 TelemetryClient。

  static ThumbnailGenerator()
{
telemetryClient = new TelemetryClient(TelemetryConfiguration.CreateDefault());
}

然后在函数中:

    [FunctionName("Upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Upload(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
string name,
ILogger log,
ExecutionContext context)
{
var evt = new EventTelemetry("Upload function called");
evt.Context.User.Id = name;
telemetryClient.TrackEvent(evt);
telemetryClient.Flush();

....

DateTime start = DateTime.UtcNow;

// Log a custom dependency in the dependencies table.
var dependency = new DependencyTelemetry
{
Name = "Upload-Image-Operation",
Timestamp = start,
Duration = DateTime.UtcNow - start,
Success = true
};

telemetryClient.TrackDependency(dependency);
telemetryClient.Flush();

return new OkObjectResult(name + "Uploaded successfully.");
}

对此的应用程序见解可以始终正确显示默认的 RequestTelemetry 和该函数的跟踪。但是,自定义事件和 DependencyTelemetry 非常不稳定,有时在应用程序洞察中显示,有时根本不显示。

enter image description here

我做了很多研究并添加了类似的内容:

    telemetryClient.Flush();

但不稳定性仍然或多或少相同。

我使用的库是:

 <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.9.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0-beta.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.25" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>

有人知道为什么吗?看起来 Azure 在这部分确实不稳定且有 bug。请给我一些提示。

最佳答案

确保您的遥测配置具有正确的仪器 key 集。我不确定 TelemetryConfiguration.CreateDefault() 是否获得了正确的值。

此外,我建议使用依赖注入(inject)在构造函数中注入(inject) TelemetryClient。它已经开箱即用。这样您就不必自己创建实例,也不必担心设置检测 key 的正确方法:

    [FunctionName("Upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Upload(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
string name,
ILogger log,
TelemetryClient telemetryClient,
ExecutionContext context)
{
telemetryClient.XXX();
...
}

刷新客户端不是必需的,仅在终止时才需要,即使如此,您也应该等待一段时间,因为它是一个异步进程。

关于c# - Azure Application Insights 不显示在 Azure 函数中创建的自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68848609/

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