- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我们正在寻找一种跨多个 API 请求 Hook Request-Id
(或 Correlation-Id
)的方法,如下图所示:
我们的想法是让一个单一的 ID 通过我们日志中的多个 API 来跟踪特定的请求。我们使用的是 .NET Core 2.1 开箱即用的标准 ILogger
。
到目前为止我们尝试了什么
我们已尝试在请求 header 中使用 Request-Id
,它成功记录,但我们无法检索该值以将其添加到其他 API 的后续 HTTP 请求中。
我们注意到还有一个被记录的 CorrelationId
。但是,我们无法弄清楚如何更新它。
此外,我们注意到 HttpContextAccessor
上有可用的 TraceIdentity
,这可能会解决我们的目的。但是,我们不知道如何利用它来解决我们的问题。
我们不能使用 Application Insights,而是希望依赖我们自己的日志基础结构框架。我们在文档中找不到太多内容。
是否有任何开箱即用的解决方案可供我们使用,而无需提出我们自己的定制解决方案?
最佳答案
我在 Twitter 上向@davidfowl 提出了同样的问题。他回复了:
No there's nothing out of the box for this. There's an end to end that works with app insights but it isn't very fleshed out. You might consider just using the same middleware across teams. if There's a work item to address this in 3.0 https://github.com/aspnet/Hosting/issues/1350
因此,从目前的情况来看,自定义中间件是唯一的出路。这可能会随着 future 的版本而改变。
更新
我们最终按照@DavidMcEleney 的建议创建了一个自定义中间件。但是,在它之上,我们将 CorrelationId
添加到 AsyncLocal
属性。这有助于我们在需要时访问代码中任何位置的 CorrelationId
。这是获取/设置 CorrelationId
的代码:
using System;
using System.Threading;
public static class CorrelationContext
{
private static readonly AsyncLocal<string> CorrelationId = new AsyncLocal<string>();
public static void SetCorrelationId(string correlationId)
{
if (string.IsNullOrWhiteSpace(correlationId))
{
throw new ArgumentException(nameof(correlationId), "Correlation id cannot be null or empty");
}
if (!string.IsNullOrWhiteSpace(CorrelationId.Value))
{
throw new InvalidOperationException("Correlation id is already set");
}
CorrelationId.Value = correlationId;
}
public static string GetCorrelationId()
{
return CorrelationId.Value;
}
}
在 CorrelationMiddleware.cs
中使用
public class CorrelationMiddleware
{
private readonly RequestDelegate _next;
public CorrelationMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
context.Request.Headers.TryGetValue("Correlation-Id-Header", out var correlationIds);
var correlationId = correlationIds.FirstOrDefault() ?? Guid.NewGuid().ToString();
CorrelationContext.SetCorrelationId(correlationId);
using (LogContext.PushProperty("Correlation-Id", correlationId))
{
await _next.Invoke(context);
}
}
}
如果我们稍后需要在代码的任何地方访问CorrelationId
,那么我们只需调用:CorrelationContext.GetCorrelationId();
关于c# - .NET Core - 使用 Request-Id 将传入请求挂接到出站 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52306882/
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How do I copy an object in Java? 我的类中有一个函数 Claus它调用一个函数来接受
任何人都可以给我一个 linux 内核驱动程序手册或示例的链接,它可以修改来自/到指定 ip 端口的传入/传出数据包吗? 谢谢! 最佳答案 Kprobes 是一种可以做到这一点的方法。这是一个使用具有
我有一个 3rd 方网站,该网站具有发送到特定 url 的 webhook。我将其设置为发送到我网站上的空白页面(例如:www.mysite.com/webhook.php) 我在 webhook.p
我对传递的约定感到困惑*args在 super().__init__()在python继承中。 我了解需要使用关键字参数 **kwargs因此,如果需要,CRO 中的类可以采用所需的参数,但为什么还有
我有一个 webapi (.NET core 3.1) 方法,它正在获取 XML 并反序列化为对象。但是一些标签的开头和结尾是不一样的。例如,如果以 打开然后它以 关闭.因此 XML 格式不正确。
是否有一个 hg 命令可以组合 hg传入 + hg传出 + hg status? 这会告诉您是否有任何远程内容需要传入,是否有任何本地提交的内容需要出去,或者是否有任何本地更改需要提交。 最佳答案 虽
据我了解,你无法在两部 iPhone 之间建立连接(如果我错了,请纠正我)。因此,我想做的是在客户端应用程序之间放置一台服务器,该服务器接受消息并将其重新分发给适当的人员。本质上,该应用程序将允许人们
我正在使用 Apache Camel 向我的 Java 服务发送消息。我在消费者路线上保留了 transacted=true。我还需要发送有关成功处理 JMS 消息的电子邮件。 我正在使用以下代码来注
这个问题在这里已经有了答案: What does "this" point to? (5 个答案) 关闭 4 年前。 public class CommandForm extends Form im
我是一名 Swift 开发人员,也是 Dart 的新手。我正在尝试编写一些通用方法。 我想知道我是否可以在 Dart 中实现类似的事情。 //Swift version public func mod
基本上有人问我是否有任何方法可以捕获和下载通过邮件服务器的所有传入和传出电子邮件(最好是文本格式)。这种下载方法需要使用PHP或类似的服务器端语言来完成。 如果我能通过上述阶段,那么最终目标就很容易实
我正在学习 Flask 并且有一个关于动态路由的问题:是否可以传入一个可接受的路由列表?我注意到 any 转换器有潜力,但很难找到它的使用示例。基本上我有不同的端点组,它们应该在它们之间触发相同的 A
我正在学习如何使用 pthread 函数。在这里我想使用 pthread_create 创建一个线程并用它计算一些数字的平均值。(数字序列末尾的0表示已经完成) 这是我到目前为止写的: #includ
我需要传递一个带有字段“nombre”作为参数的条件 目前我这样做: @xcondicion= {:nombre.like => "%#{params[:qyBusqueda]}%"} cliente
我有以下两个 parseInt(),我不太清楚为什么它们会给我不同的结果: alert(parseInt(0.00001)) 显示 0; alert(parseInt(0.00000001)) 显示
我有一个使用 SODA 包来操作 JSON 数据库的 PL/SQL 处理程序。我想: 读取有效负载中键 id 的值 将负载 JSON 写入数据库中的新文档。 要执行第 1 步,处理程序将 :body
我正在使用 Oracle 试用 Dapper,我试图运行多结果集查询,但 Oracle 需要 dbtype 的 refcursor。 StringBuilder query = new StringB
我在将“工作表”选项卡重命名为任何文件名时遇到问题。这样做的问题是,如果用户意外下载工作表两次,Windows 会将 template(1).csv 附加到文件中,以保持其下载目录中的唯一性。在 Ex
我将 id 作为字符串传递到函数中。我想使用该 ID 来更改背景颜色。我用过: $("#"+address).css("background-color", "#3399FF"); 但它没有改变背景颜
我有一个 ListSourceAdapter.java 类, class ListSourceViewHolder extends RecyclerView.ViewHolder implements
我是一名优秀的程序员,十分优秀!