我正在实现自定义 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" };
我的示例代码如下,仅更改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 ");
}
}
After executed, you can see the operation_id for trace / exception in azure portal:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)