From 9d0a453a0c2e17f155f4da72d94af622671db29d Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 10 Apr 2018 20:06:24 +0800 Subject: [PATCH] add tracingheader helper func --- src/DotNetCore.CAP/Infrastructure/Helper.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/DotNetCore.CAP/Infrastructure/Helper.cs b/src/DotNetCore.CAP/Infrastructure/Helper.cs index 92ae6fb..e67f41c 100644 --- a/src/DotNetCore.CAP/Infrastructure/Helper.cs +++ b/src/DotNetCore.CAP/Infrastructure/Helper.cs @@ -4,6 +4,7 @@ using System; using System.ComponentModel; using System.Reflection; +using DotNetCore.CAP.Diagnostics; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -48,7 +49,7 @@ namespace DotNetCore.CAP.Infrastructure public static long ToTimestamp(DateTime value) { var elapsedTime = value - Epoch; - return (long) elapsedTime.TotalSeconds; + return (long)elapsedTime.TotalSeconds; } @@ -90,6 +91,12 @@ namespace DotNetCore.CAP.Infrastructure return AddJsonProperty(json, "ExceptionMessage", jObject); } + public static string AddTracingHeaderProperty(string json, TracingHeaders headers) + { + var jObject = ToJObject(headers); + return AddJsonProperty(json, "TracingHeaders", jObject); + } + public static bool IsInnerIP(string ipAddress) { bool isInnerIp; @@ -156,6 +163,16 @@ namespace DotNetCore.CAP.Infrastructure }); } + private static JObject ToJObject(TracingHeaders headers) + { + var jobj = new JObject(); + foreach (var keyValuePair in headers) + { + jobj[keyValuePair.Key] = keyValuePair.Value; + } + return jobj; + } + private static string AddJsonProperty(string json, string propertyName, JObject propertyValue) { var jObj = JObject.Parse(json);