在我的项目中,架构非常简单:一些 Azure Functions
由 Servicebus
触发。
比如我有一个发票流程:
每个步骤都是一个 Azure 函数。当函数完成创建发票 (1) 时,它会在服务总线中发送一条消息以触发为发票创建 PDF (2)。然后,如果付款成功,该函数将发送一条消息将发票标记为已付款。
我看到了这段代码
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace WebApi.CorrelationId.MessageHandlers
{
public class AddCorrelationIdToResponseHandler : DelegatingHandler
{
private const string CorrelationIdHeaderName = "X-Correlation-Id";
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
var responseMessage = await base.SendAsync(request, cancellationToken);
responseMessage
.Headers
.Add(CorrelationIdHeaderName, request.GetCorrelationId().ToString());
return responseMessage;
}
}
}
然后,在配置代码中
var httpConfiguration = new HttpConfiguration();
// your regular configuration
httpConfiguration.MessageHandlers.Add(new AddCorrelationIdToResponseHandler());
我不确定 Azure Functions 中是否有有效的 HttpConfiguration
。
如何使用 CorrelationId 跟踪所有步骤?
最佳答案
您可以尝试使用 Microsoft.Azure.WebJobs.ExecutionContext
中的 InvocationId
。这将返回一个 Guid,您可以使用它来关联您的日志。在这里进一步阅读:https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#executioncontext
关于azure-functions - Azure Functions、Servicebus 和 CorrelationId 的一致方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62515674/
相关文章:
javascript - 为什么字符串中的等号会抛出意外的标记错误?
ios - VoiceOver 在 UITableView 中滚动,其中单元格和 subview 都
javascript - React 如何区分具有相同父级的两个数组的相似键?
python - Librosa 无法从 BytesIO 加载
python - Not able to "import win32com.shell.shell"
flutter - 在 Flutter 应用程序中从 pubspec for web 获取版本
react-native - 应用程序关闭时无法更新徽章计数 - react native iOS
javascript - Postman 中的 API 工作正常但在 React Native 中出