ApplicationInsights OperationId为空 [英] ApplicationInsights OperationId is empty
问题描述
我正在实现自定义ApplicationInsights记录器,并能够在跟踪,异常和请求等写入位置写入所有日志,但是OperationId在跟踪和异常中为空.
I'm implementing custom ApplicationInsights logger and able to write all logs in write places like traces, exceptions and request, but OperationId is empty in traces and exceptions.
昨天我在所有表中使用相同的代码并获取OperationId.之后,我在多线程方案中工作,效果不佳.现在,我再次从简单的代码开始,但是看不到OperationId.
Yesterday I was using same code and getting OperationId in all tables. After that I was playing for multi-thread scenario which didn't work well. Now I started again with simple code but can't see 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 ");
}
}
推荐答案
此问题非常棘手,这是由于检测键设置所致.如果您使用 Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
(您在代码中使用过)来设置检测密钥,那么在应用程序见解中将不会显示operation_id.
This issue is very tricky, it's due to the instrumentation key setting.
If you use Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
(you used in your code) to set instrumentation key, then no operation_id appears in app insights.
因此,请使用以下代码行设置检测键:
So please use this line of code to set instrumentation key:
TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = "your_key" };
我的示例代码如下,仅更改检测键设置方法:
My sample code as below, only change the instrumentation key setting method:
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门户中看到trace/exception的operation_id:
After executed, you can see the operation_id for trace / exception in azure portal:
这篇关于ApplicationInsights OperationId为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!