我正在实现自定义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 ");
}
}
最佳答案
这个问题非常棘手,这是由于工具键设置引起的。
如果您使用Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
(您在代码中使用的)来设置检测键,则应用见解中将不会显示operation_id。
因此,请使用以下代码行设置检测键:
TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = "your_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门户中看到trace / exception的operation_id: