我在net Framework 4.8上创建了一个http触发器V1 azure函数,并使用ILogger进行日志记录。代码是这样的。
我检查了 Application Insight 并查询了跟踪表。该表包含名为 customDimensions 和 operation_ParentId 的列。请问是否可以在 customDimensions 列中添加自定义属性,或为 operation_ParentId 设置新的 Guid 值?我知道我可以使用 TelemetryClient sdk 创建用于日志记录的自定义遥测客户端。只是好奇是否有任何简单的方法不需要创建新的遥测客户端,因为 azure 函数提供了与应用程序洞察的内置集成。
此外,由于 azure 函数运行时会自动跟踪请求,因此是否也可以更改 requests 表的 operation_ParentId 和 customDimensions ?非常感谢!
最佳答案
要获取 header 和 App Insights 来获取自定义操作 ID,必须重写两件事。
第一个是包装 HttpClient
的 Activity,负责控制相关 header ,另一个是 App Insights 的依赖项跟踪。
虽然您可以在 HttpClient 中使用 disable Actions completely,但您可以通过设置 Activity.Current = null
来删除客户端中的 for this request 以限制副作用。
var operationId = "CR" + Guid.NewGuid().ToString();
var url = "https://www.microsoft.com";
using (var client = new HttpClient())
{
using (var requestMessage =
new HttpRequestMessage(HttpMethod.Get, url))
{
//Makes the headers configurable
Activity.Current = null;
//set correlation header manually
requestMessage.Headers.Add("Request-Id", operationId);
await client.SendAsync(requestMessage);
}
}
下一步是删除 App Insights 默认跟踪 filter out the default telemetry 。同样,您可以完全禁用依赖项跟踪,也可以针对此请求使用 TelemetryClient.TrackDependency()
。就像初始化器一样,处理器在 Startup 类中注册。
services.AddApplicationInsightsTelemetryProcessor<CustomFilter>();
public class CustomFilter : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
// next will point to the next TelemetryProcessor in the chain.
public CustomFilter(ITelemetryProcessor next)
{
this.Next = next;
}
public void Process(ITelemetry item)
{
// To filter out an item, return without calling the next processor.
if (!OKtoSend(item)) { return; }
this.Next.Process(item);
}
// Example: replace with your own criteria.
private bool OKtoSend(ITelemetry item)
{
var dependency = item as DependencyTelemetry;
if (dependency == null) return true;
if (dependency.Type == "Http"
&& dependency.Data.Contains("microsoft.com")
//This key is just there to help identify the custom tracking
&& !dependency.Context.GlobalProperties.ContainsKey("keep"))
{
return false;
}
return true;
}
}
最后,您必须注入(inject)遥测客户端并在进行远程调用的方法中调用 ojit_a。
var operationId = "CR" + Guid.NewGuid().ToString();
//setup telemetry client
telemetry.Context.Operation.Id = operationId;
if (!telemetry.Context.GlobalProperties.ContainsKey("keep"))
{
telemetry.Context.GlobalProperties.Add("keep", "true");
}
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
//continue setting up context if needed
var url = "https:microsoft.com";
using (var client = new HttpClient())
{
//Makes the headers configurable
Activity.Current = null;
using (var requestMessage =
new HttpRequestMessage(HttpMethod.Get, url))
{
//Makes the headers configurable
Activity.Current = null;
//set header manually
requestMessage.Headers.Add("Request-Id", operationId);
await client.SendAsync(requestMessage);
}
}
//send custom telemetry
telemetry.TrackDependency("Http", url, "myCall", startTime, timer.Elapsed, true);
请参阅此处了解更多信息。
注意:通过禁用内置依赖项跟踪和 App Insights 并自行处理,可以实现上述情况。但更好的方法是让 .NET Core 和 App Insights 进行跟踪。
关于azure - 如何为Azure函数日志添加customDimensions并设置操作_parentId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71965086/