我正在使用自定义中间件来记录来自 Web API 的请求/响应。使用
TrackRequest(requestTelemetry)
,我在custom properties
中添加一些operation_name
和requestTelemetry
。当我在 Azure 应用服务中托管 Web API 时,我可以看到请求表中记录的操作 ID,但是当我在 Service Fabric (Kestrel) 中托管相同的代码时,请求遥测没有操作 ID,只有依赖项遥测有操作 ID。
之前完成了很多代码更改
operation_id
,并且parentOperation_id
写入了请求表和依赖表中。由于一些代码更改,现在 operation_id 和 ParentOperation_Id 未写入 AI 表中。
有没有
config/code
让AI自动记录operationId
。一些文章提到,使用 TrackRequest,servcie 代码必须显式添加操作 ID,但之前不认为任何显式添加已完成,并且操作 ID 仍然被记录。
我能够在本地痕迹中看到
operation_id
和 parentOperation_id
。
但最初无法在 Application Insights 中看到相同的跟踪。
在
Transaction Search
=> 单击任意 trace/request
=> 单击“显示全部”。
现在我能够看到与我在本地看到的相同的痕迹。
一些文章提到,使用TrackRequest,servcie代码必须显式添加操作_Id,
如MSDoc中所述,我们显式添加代码来记录
operation_id
和parentOperation_id
。
使用以下代码显式记录
operation_id
和 parentOperation_id
。
Controller.cs
文件:
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace OperationID.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly TelemetryClient _tc;
public WeatherForecastController(ILogger<WeatherForecastController> logger, TelemetryClient tc)
{
_logger = logger;
_tc = tc;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
var operation_id = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
var parentOperation_id = Activity.Current?.ParentId ?? HttpContext.TraceIdentifier;
_tc.TrackTrace($"operation_id: {operation_id}, parentOperation_id: {parentOperation_id}");
_logger.LogInformation($"Log operation_id: {operation_id}, parentOperation_id: {parentOperation_id}");
var requestTelemetry = HttpContext.Features.Get<RequestTelemetry>();
_tc.TrackRequest(requestTelemetry);
_logger.LogInformation("GetWeatherForecast Logs");
----------
----------
}
}
}