English 中文(简体)
阅读和宣传 追查 Id in original ASP. NET 4.8
原标题:Read and propagate TraceId in old ASP.NET 4.8
  • 时间:2020-06-03 07:11:09
  •  标签:
  • c#
  • asp.net

I m 保留协会撰写的若干申请。 NET 4.8(完整框架)和ASP。 NET Core。

。 NET Core implementings https://www.w3.org/TR/trace-context/。 因此,我可以看到我的标志(我利用Serilog进行有条理的伐木),这些记录丰富了父母Id、SpanId和最重要的追踪Id。

如何在旧的协会中读和推广追查。 NET 4.8 申请

My applications are doing quite a lot of requests between each other and this would greatly improve debugging experience. Unfortunately most of the requests originate on the old ASP.NET 4.8 apps and go to newer .NET Core ones.

理想的情况是,我要像非洲伙伴关系一样进入同样状态。 NET 核心数据是——如果请求书来自吉大港山区头目,则根据该要求使用并填入父母Id和SpanId。 此外,还进一步向源自吉大港山区的其他申请进行宣传。 NET Core app。

Thanks!

最佳回答

因此,我设法自行解决该问题。

首先,需要增加<条码>系统。

接着,创建“代号” 班级:

public static class ActivityManager
{
    public static void StartActivity()
    {
        if (Activity.Current == null)
        {
            var activity = new Activity("Default Activity");

            string parentIdFromHeaders = HttpContext.Current?.Request.Headers[GetRequestIdHeaderName()];
            if (!string.IsNullOrEmpty(parentIdFromHeaders))
            {
                activity.SetParentId(parentIdFromHeaders);
            }

            activity.Start();
            Activity.Current = activity;

            // Sometimes I had issues with Activity.Current being empty even though I set it
            // So just to be sure, I add it also to HttpContext Items.
            HttpContext.Current?.Items.Add("Activity", activity);
        }
    }

    public static void StopActivity()
    {
        GetActivity()?.Stop();
    }

    public static Activity GetActivity()
    {
        Activity activity = Activity.Current ?? (Activity)HttpContext.Current.Items["Activity"];
        return activity;
    }

    public static string GetRequestIdHeaderName()
    {
        return "Request-Id";
    }

    public static string GetRequestId()
    {
        Activity activity = GetActivity();

        if (activity != null)
        {
            string activityId = activity.Id;
            return activityId;
        }

        // For the rare cases when something happens and activity is not set
        // Try to read Request-Id first, if none, then create new GUID
        return HttpContext.Current?.Request.Headers.Get(GetRequestIdHeaderName())
                ?? Guid.NewGuid().ToString().Replace("-", "");
    }
}

www.un.org/Depts/DGACM/index_french.htm 秘书

protected void Application_Start()
{
    Activity.DefaultIdFormat = ActivityIdFormat.Hierarchical;
}

protected void Application_BeginRequest()
{
    ActivityManager.StartActivity();
}

protected void Application_EndRequest()
{
    ActivityManager.StopActivity();
}

现在所有新收到的申请都建立了新的<条码> 积极性,并适当建立了<条码>。 Id 。

为在伐木环境中添加<条码>SpanId,TraceIdParentId

public class TraceLogEnricher : ILogEventEnricher
{
    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
    {
        Activity activity = ActivityManager.GetActivity();

        if (activity != null)
        {
            string parentId = activity.ParentId;
            string rootId = activity.RootId;
            string activityId = activity.Id;

            logEvent.AddPropertyIfAbsent(new LogEventProperty("SpanId", new ScalarValue(activityId)));
            logEvent.AddPropertyIfAbsent(new LogEventProperty("ParentId", new ScalarValue(parentId)));
            logEvent.AddPropertyIfAbsent(new LogEventProperty("TraceId", new ScalarValue(rootId)));           }
    }
}

and added it to Serilog configuration with .Enrich.With<TraceLogEnricher>().

最后一个步骤是整理即将提出的申请。 在<代码>HttpClient上,DefaultRequestHeaders。 (为了方便起见,也可在IHttpClientFactory配置中加以配置。)

var requestId = ActivityManager.GetRequestId();
client.DefaultRequestHeaders.Add("Request-Id", requestId);

如果有 使用网络服务,最好添加<代码>。 请求-Id也针对网络服务申请。 为此,网络服务客户的压倒性<代码>GetWebRequest方法。 (这些产品通常作为部分类别产生,因此在你自己的部分类别档案中优先于部分类别)。

protected override WebRequest GetWebRequest(Uri uri)
{
    var request = base.GetWebRequest(uri);

    request.Headers.Add("Request-Id", ActivityManager.GetRequestId());
    
    return request;
}
问题回答

感谢你分享这一解决办法,即试图做类似的事情,在我的申请中添加了该守则,但真正的道歉并不 trace。 你们是否也想分享你的eri? i 只想看到,我是否在我的证言模板中犯过错误。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签