在ASP.NET Core 2.1中System.Diagnostics.Activity为null [英] System.Diagnostics.Activity is null in aspnet core 2.1

查看:326
本文介绍了在ASP.NET Core 2.1中System.Diagnostics.Activity为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚刚将aspnet core 2.0应用程序更新为2.1,并且在使用/依赖 System.Diagnostics.Activity 时遇到了问题。

We've just updated an aspnet core 2.0 application to 2.1 and have run into a problem with our usage/reliance on System.Diagnostics.Activity.

背景

我们希望在我们的服务范围内传递一致的相关ID,以便我们可以关联每个请求的日志条目。

We want a consistent 'correlation id' passed across our service boundaries, so that we can correlate log entries per request.

我们采用的方法是:


  • 将诊断侦听器添加到 Microsoft.AspNetCore.Hosting.HttpRequestIn.Start

调用此侦听器时,请检查context.request中是否有一个名为 Request-Id的标头,如果没有,则添加一个值为 Activity.Current的标头。 ID

When this listener was invoked, check if there was a header in the context.request called "Request-Id", and if there wasn't then add one with a value of Activity.Current.Id

另一中间件负责将 Request-Id标头值推送到日志记录上下文中

Another piece of middleware took care of pushing the "Request-Id" header value into the logging context

在2.0版本和 Activity.Current.Id 意味着我们可以关联不同服务和层之间的日志条目。在2.1中,我们现在遇到了异常,因为 Activity.Current 在请求的进入点上似乎总是为空(即,在此请求中的第一个服务)一个API)。

This worked just fine in 2.0 and the hierarchical nature of Activity.Current.Id meant we could correlate log entries across different services and layers. In 2.1 we are now getting exceptions because Activity.Current appears to always be null on the point-of-entry for a request (i.e. the first service that is hit, in this case an API).

我没有找到任何表明HttpRequest进入时不再自动开始活动的信息,但这似乎正在发生。是否有人能够揭示发生了什么变化和/或我们做错了什么?

I've not managed to find any information that suggests that an activity is no longer automatically started whenever an HttpRequest comes in, but that's what it seems like is happening. Is anyone able to shed any light on what has changed and/or what we're doing wrong?

某些代码

启动配置方法...

public void Configure(IApplicationBuilder app, IHostingEnvironment env, DiagnosticListener diagnosticListener)
        {
           diagnosticListener.SubscribeWithAdapter(new HttpRequestInDiagnosticListener());
           app.UseMiddleware<SetRequestIdHeaderForHttpRequestInMiddleware>();

            if (env.IsDevelopment())
                app.UseDeveloperExceptionPage();
            else
                app.UseStatusCodePages();

            app.UseSecurityHeaders(Headers.AddSecurityHeaders());

            app.UseMvc();
        }

...以及所涉及的自定义类

... and the custom classes involved

    public class HttpRequestInDiagnosticListener
    {
        [DiagnosticName("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start")]
        public virtual void OnMiddlewareStarting(HttpContext httpContext)
        {
            Console.WriteLine($"Middleware Starting, path: {httpContext.Request.Path}");
        }
    }

    public class SetRequestIdHeaderForHttpRequestInMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly DiagnosticSource _diagnostics;

        public SetRequestIdHeaderForHttpRequestInMiddleware(RequestDelegate next, DiagnosticSource diagnosticSource)
        {
            _next = next;
            _diagnostics = diagnosticSource;
        }

        public async Task Invoke(HttpContext context)
        {

            if (_diagnostics.IsEnabled("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start"))
            {
                if (!context.Request.Headers.Keys.Contains("Request-Id"))
                {
                    context.Request.Headers.Add("Request-Id", Activity.Current.Id);
                }
            }

            await _next.Invoke(context);
        }
    }


推荐答案

回答经过数天的研究后,我自己提出了一个问题,但很简单:是的:在2.1中,活动不再像在2.0中那样开始。
https://github.com/aspnet/Hosting/blob/release/2.1/src/Microsoft.AspNetCore.Hosting/Internal/HostingApplicationDiagnostics.cs#L54-L79

Answering my own question, after days of looking into this, but succinctly YES: in 2.1 activities are no longer started in the same way that they were in 2.0. https://github.com/aspnet/Hosting/blob/release/2.1/src/Microsoft.AspNetCore.Hosting/Internal/HostingApplicationDiagnostics.cs#L54-L79

要启动活动,您需要特别观察 Microsoft.AspNetCore.Hosting.HttpRequestIn ,而问题中的代码正在观察 Microsoft.AspNetCore.Hosting.HttpRequestIn.Start

For an Activity to start you need to specifically be observing Microsoft.AspNetCore.Hosting.HttpRequestIn, whereas the code in the question was observing Microsoft.AspNetCore.Hosting.HttpRequestIn.Start

不幸的是,这是' 2.1中唯一影响问题代码的更改,仅更改观察到的活动名称并不能使此代码正常工作,因为2.1现在可以处理 Request-Id 标头和 CorrelationId 以自己的方式记录属性,这会干扰此中间件代码。

Unfortunately that isn't the only change in 2.1 that has affected the code in the question, and just changing the Activity Name being observed doesn't make this code work as 2.1 now handles Request-Id headers and CorrelationId log properties in its own way which interferes with this middleware code.

这篇关于在ASP.NET Core 2.1中System.Diagnostics.Activity为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆