gpt4 book ai didi

c# - ApplicationInsights OperationId 为空

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:09 24 4
gpt4 key购买 nike

我正在实现自定义 ApplicationInsights 记录器,并且能够在跟踪、异常和请求等写入位置写入所有日志,但跟踪和异常中的 OperationId 为空。

昨天我使用相同的代码并在所有表中获取OperationId。之后我玩了多线程场景,但效果不佳。现在我再次开始使用简单的代码,但看不到OperationId。

我的代码有什么问题吗?

public static class Function2
{
private static TelemetryClient telemetryClient = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
{
InstrumentationKey = "********-****-********-****"
});

[FunctionName("Function2")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage req)
{
RequestTelemetry requestTelemetry = new RequestTelemetry { Name = "Function2" };
var operation = telemetryClient.StartOperation(requestTelemetry);

telemetryClient.TrackTrace("trace message", SeverityLevel.Error);
telemetryClient.TrackException(new System.Exception("My custom exception"));


operation.Telemetry.Success = true;
telemetryClient.StopOperation(operation);

return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}
}

最佳答案

这个问题非常棘手,这是由于仪器 key 设置造成的。如果您使用 Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration(您在代码中使用的)来设置检测 key ,则应用洞察中不会出现 operation_id。

所以请使用这行代码来设置检测 key :

TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = "your_key" };

我的示例代码如下,仅更改instrumentation key设置方法:

public static class Function1
{

private static TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = "your_key" };

[FunctionName("Function2")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage req)
{
RequestTelemetry requestTelemetry = new RequestTelemetry { Name = "Function211" };
var operation = telemetryClient.StartOperation(requestTelemetry);

telemetryClient.TrackTrace("trace message 111", SeverityLevel.Error);
telemetryClient.TrackException(new System.Exception("My custom exception 111"));

operation.Telemetry.Success = true;
telemetryClient.StopOperation(operation);

return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}
}

执行后,您可以在azure门户中看到跟踪/异常的操作_id: enter image description here

关于c# - ApplicationInsights OperationId 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52688906/

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