C# 如何在HTTPTrigger Azure函数3.x中获取操作ID。附图

C# 如何在HTTPTrigger Azure函数3.x中获取操作ID。附图,c#,azure,appinsights,C#,Azure,Appinsights,我们需要HTTP触发器Azure函数中的OperationId值。我们怎样才能得到它 您可以使用Activity.Current.RootId在门户中的HTTP触发器内获取操作Id 代码: #r "Newtonsoft.Json" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; using Syste

我们需要HTTP触发器Azure函数中的OperationId值。我们怎样才能得到它


您可以使用
Activity.Current.RootId
在门户中的HTTP触发器内获取操作Id

代码:

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Diagnostics;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
    log.LogInformation($"Activity Current RootId:{Activity.Current.RootId}");

    string operationId = Activity.Current.RootId;
    return new OkObjectResult("success");
}
#r“Newtonsoft.Json”
Net系统;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Primitives;
使用Newtonsoft.Json;
使用系统诊断;
公共静态异步任务运行(HttpRequest请求,ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
log.LogInformation($“活动当前根ID:{Activity.Current.RootId}”);
字符串操作ID=Activity.Current.RootId;
返回新的OkObjectResult(“成功”);
}

您可以将执行上下文添加到函数中

大概是这样的:

[FunctionName("HttpTriggerCSharp")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
    HttpRequest req, ILogger log,ExecutionContext context)
[FunctionName(“HttpTriggerCSharp”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]
HttpRequest请求、ILogger日志、ExecutionContext(上下文)

您可以访问
Context.InvocationId
,这将是您的需求(operationid)。

我已经测试过了。它与operationid不同。您可以试一试。您可以看看这个。Acivity.Current.RootId正在获取值。但是,Activity.Current返回null。请帮忙。已附加图像:获取操作ID。png@Saranyan这是一只虫子。您可以直接在门户中创建函数并使用此代码。您好,赵,我试着在门户中创建函数,并使用了给定的代码。但是,它仍然给出了空引用异常。如果有其他方法,请告诉我,TQ..@Saranyan我更新了我的答案。你喜欢这样做吗?谢谢赵,我在门户中创建了函数,我正在获取操作ID。所有my Azure函数都是使用Visual Studio 2019编写的,而不是直接在门户中编写。如何获取操作ID。您有什么解决方案吗?谢谢你的回复。