Skip to content

Commit 297eef0

Browse files
committed
Code clean-up.
1 parent 64ad62a commit 297eef0

2 files changed

Lines changed: 67 additions & 53 deletions

File tree

Diagnostics/PostSharp.Samples.Logging.ElasticStack/ClientExample/InstrumentOutgoingRequestsAspect.cs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ClientExample;
22
using PostSharp.Aspects;
33
using PostSharp.Patterns.Diagnostics;
4+
using PostSharp.Patterns.Diagnostics.Contexts;
45
using PostSharp.Patterns.Formatters;
56
using PostSharp.Serialization;
67
using System;
@@ -44,45 +45,10 @@ public override async Task OnInvokeAsync(MethodInterceptionArgs args)
4445

4546

4647
// 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)
5050
{
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);
8652
}
8753

8854

@@ -129,6 +95,49 @@ public override async Task OnInvokeAsync(MethodInterceptionArgs args)
12995
}
13096
}
13197

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+
132141
private static string Trim(string s, string suffix)
133142
=> s.EndsWith(suffix) ? s.Substring(0, s.Length - suffix.Length) : s;
134143
}

Diagnostics/PostSharp.Samples.Logging.ElasticStack/MicroserviceExample/LoggingActionFilter.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
3535
string correlationContext = context.HttpContext.Request.Headers["Correlation-Context"];
3636
if (!string.IsNullOrEmpty(correlationContext))
3737
{
38-
var properties = new List<LoggingProperty>();
39-
foreach (var pair in correlationContext.Split(',', StringSplitOptions.RemoveEmptyEntries))
40-
{
41-
var posOfEqual = pair.IndexOf('=');
42-
if (posOfEqual <= 0)
43-
{
44-
continue;
45-
}
46-
47-
var propertyName = pair.Substring(0, posOfEqual);
48-
var propertyValue = pair.Substring(posOfEqual + 1);
49-
properties.Add(new LoggingProperty(propertyName, propertyValue) { IsBaggage = true });
50-
}
51-
52-
options.Properties = properties.ToArray();
38+
options.Properties = ParseCorrelationContext(correlationContext)?.ToArray();
5339
}
5440

5541
var request = context.HttpContext.Request;
@@ -79,5 +65,24 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
7965
}
8066
}
8167
}
68+
69+
private static List<LoggingProperty> ParseCorrelationContext(string correlationContext)
70+
{
71+
var properties = new List<LoggingProperty>();
72+
foreach (var pair in correlationContext.Split(',', StringSplitOptions.RemoveEmptyEntries))
73+
{
74+
var posOfEqual = pair.IndexOf('=');
75+
if (posOfEqual <= 0)
76+
{
77+
continue;
78+
}
79+
80+
var propertyName = pair.Substring(0, posOfEqual);
81+
var propertyValue = pair.Substring(posOfEqual + 1);
82+
properties.Add(new LoggingProperty(propertyName, propertyValue) { IsBaggage = true });
83+
}
84+
85+
return properties;
86+
}
8287
}
8388
}

0 commit comments

Comments
 (0)