|
1 | 1 | using ClientExample; |
2 | 2 | using PostSharp.Aspects; |
3 | 3 | using PostSharp.Patterns.Diagnostics; |
| 4 | +using PostSharp.Patterns.Diagnostics.Contexts; |
4 | 5 | using PostSharp.Patterns.Formatters; |
5 | 6 | using PostSharp.Serialization; |
6 | 7 | using System; |
@@ -44,45 +45,10 @@ public override async Task OnInvokeAsync(MethodInterceptionArgs args) |
44 | 45 |
|
45 | 46 |
|
46 | 47 | // Generate the Correlation-Context header. |
47 | | - UnsafeStringBuilder correlationContextBuilder = null; |
48 | | - var propertyNames = new HashSet<string>(); |
49 | | - try |
| 48 | + var correlationContext = GetCorrelationContext(http, activity.Context); |
| 49 | + if (correlationContext != null) |
50 | 50 | { |
51 | | - activity.Context.ForEachProperty((LoggingProperty property, object value, ref object _) => |
52 | | - { |
53 | | - if (!property.IsBaggage || !propertyNames.Add(property.Name)) |
54 | | - { |
55 | | - return; |
56 | | - } |
57 | | - |
58 | | - if (correlationContextBuilder == null) |
59 | | - { |
60 | | - propertyNames = new HashSet<string>(); |
61 | | - correlationContextBuilder = new UnsafeStringBuilder(1024); |
62 | | - } |
63 | | - |
64 | | - if (correlationContextBuilder.Length > 0) |
65 | | - { |
66 | | - correlationContextBuilder.Append(", "); |
67 | | - } |
68 | | - |
69 | | - correlationContextBuilder.Append(property.Name); |
70 | | - correlationContextBuilder.Append('='); |
71 | | - |
72 | | - var formatter = |
73 | | - property.Formatter ?? LoggingServices.Formatters.Get(value.GetType()); |
74 | | - |
75 | | - formatter.Write(correlationContextBuilder, value); |
76 | | - }); |
77 | | - |
78 | | - if (correlationContextBuilder != null) |
79 | | - { |
80 | | - http.DefaultRequestHeaders.Add("Correlation-Context", correlationContextBuilder.ToString()); |
81 | | - } |
82 | | - } |
83 | | - finally |
84 | | - { |
85 | | - correlationContextBuilder?.Dispose(); |
| 51 | + http.DefaultRequestHeaders.Add("Correlation-Context", correlationContext); |
86 | 52 | } |
87 | 53 |
|
88 | 54 |
|
@@ -129,6 +95,49 @@ public override async Task OnInvokeAsync(MethodInterceptionArgs args) |
129 | 95 | } |
130 | 96 | } |
131 | 97 |
|
| 98 | + private static string GetCorrelationContext(HttpClient http, ILoggingContext context) |
| 99 | + { |
| 100 | + UnsafeStringBuilder correlationContextBuilder = null; |
| 101 | + var propertyNames = new HashSet<string>(); |
| 102 | + try |
| 103 | + { |
| 104 | + context.ForEachProperty((LoggingProperty property, object value, ref object _) => |
| 105 | + { |
| 106 | + if (!property.IsBaggage || !propertyNames.Add(property.Name)) |
| 107 | + { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (correlationContextBuilder == null) |
| 112 | + { |
| 113 | + propertyNames = new HashSet<string>(); |
| 114 | + correlationContextBuilder = new UnsafeStringBuilder(1024); |
| 115 | + } |
| 116 | + |
| 117 | + if (correlationContextBuilder.Length > 0) |
| 118 | + { |
| 119 | + correlationContextBuilder.Append(", "); |
| 120 | + } |
| 121 | + |
| 122 | + correlationContextBuilder.Append(property.Name); |
| 123 | + correlationContextBuilder.Append('='); |
| 124 | + |
| 125 | + var formatter = |
| 126 | + property.Formatter ?? LoggingServices.Formatters.Get(value.GetType()); |
| 127 | + |
| 128 | + formatter.Write(correlationContextBuilder, value); |
| 129 | + }); |
| 130 | + |
| 131 | + return correlationContextBuilder?.ToString(); |
| 132 | + |
| 133 | + } |
| 134 | + finally |
| 135 | + { |
| 136 | + correlationContextBuilder?.Dispose(); |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
132 | 141 | private static string Trim(string s, string suffix) |
133 | 142 | => s.EndsWith(suffix) ? s.Substring(0, s.Length - suffix.Length) : s; |
134 | 143 | } |
|
0 commit comments