Skip to content

Commit e5089b0

Browse files
committed
Expand integration testing to cover the context + structured log args case
1 parent 502ebaa commit e5089b0

12 files changed

Lines changed: 316 additions & 50 deletions

File tree

tests/Agent/IntegrationTests/IntegrationTests/Logging/StructuredLogArgContextDataTests.cs

Lines changed: 196 additions & 50 deletions
Large diffs are not rendered by default.

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/DummyMELAdapter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public void InfoWithStructuredArgs(string messageTemplate, object[] args)
6161
_logger.LogInformation(messageTemplate, args);
6262
}
6363

64+
public void InfoWithStructuredArgsAndContextDictionary(string messageTemplate, object[] args, Dictionary<string, object> context)
65+
{
66+
using (_logger.BeginScope(context))
67+
{
68+
_logger.LogInformation(messageTemplate, args);
69+
}
70+
}
71+
6472
public void LogMessageInNestedScopes()
6573
{
6674
using (var _ = _logger.BeginScope("{ScopeKey1}", "scopeValue1"))

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/ILoggingAdapter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface ILoggingAdapter
2121
public void InfoWithContextDictionary(string message, Dictionary<string, object> context);
2222
public void InfoWithObjectParameter(string message, object param);
2323
public void InfoWithStructuredArgs(string messageTemplate, object[] args);
24+
public void InfoWithStructuredArgsAndContextDictionary(string messageTemplate, object[] args, Dictionary<string, object> context);
2425
public void LogMessageInNestedScopes();
2526

2627

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/Log4NetLoggingAdapter.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ public void InfoWithStructuredArgs(string messageTemplate, object[] args)
9191
_log.InfoFormat(messageTemplate, args);
9292
}
9393

94+
public void InfoWithStructuredArgsAndContextDictionary(string messageTemplate, object[] args, Dictionary<string, object> context)
95+
{
96+
var logEventData = new LoggingEventData()
97+
{
98+
Message = string.Format(messageTemplate, args),
99+
Level = Level.Info
100+
};
101+
102+
var logEvent = new LoggingEvent(logEventData);
103+
if (context.Count > 0)
104+
{
105+
var keys = new List<string>(context.Keys);
106+
107+
logEvent.Properties[keys[0]] = context[keys[0]];
108+
logEvent.Properties[keys[1]] = context[keys[1]];
109+
110+
log4net.GlobalContext.Properties[keys[2]] = context[keys[2]];
111+
log4net.ThreadContext.Properties[keys[3]] = context[keys[3]];
112+
}
113+
114+
_log.Logger.Log(logEvent);
115+
}
116+
94117
public void LogMessageInNestedScopes()
95118
{
96119
throw new NotImplementedException();

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/LoggingTester.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,36 @@ public static void CreateSingleLogMessageWithStructuredArgs(string messageTempla
206206
});
207207
}
208208

209+
[LibraryMethod]
210+
[Transaction]
211+
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
212+
public static void CreateSingleLogMessageWithStructuredArgsAndContext(string messageTemplate, string argString, string context = null)
213+
{
214+
var args = argString.Split(',').Select(a => (object)a).ToArray();
215+
216+
var contextDict = new Dictionary<string, object>();
217+
218+
if (!string.IsNullOrEmpty(context))
219+
{
220+
var array = context.Split(',');
221+
222+
foreach (var item in array)
223+
{
224+
var pairs = item.Split('=');
225+
226+
if (!contextDict.ContainsKey(pairs[0]))
227+
{
228+
contextDict.Add(pairs[0], pairs[1]);
229+
}
230+
}
231+
}
232+
_logs.Values.ToList().ForEach(l =>
233+
{
234+
l.InfoWithStructuredArgsAndContextDictionary(messageTemplate, args, contextDict);
235+
});
236+
237+
}
238+
209239
[LibraryMethod]
210240
[Transaction]
211241
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/MelLoggingAdapter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ public void InfoWithStructuredArgs(string messageTemplate, object[] args)
7272
{
7373
logger.LogInformation(messageTemplate, args);
7474
}
75+
public void InfoWithStructuredArgsAndContextDictionary(string messageTemplate, object[] args, Dictionary<string, object> context)
76+
{
77+
using (logger.BeginScope(context))
78+
{
79+
logger.LogInformation(messageTemplate, args);
80+
}
81+
}
7582

7683
public void LogMessageInNestedScopes()
7784
{

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/NLogExtensionsLoggingAdapter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public void InfoWithStructuredArgs(string messageTemplate, object[] args)
7474
logger.LogInformation(messageTemplate, args);
7575
}
7676

77+
public void InfoWithStructuredArgsAndContextDictionary(string messageTemplate, object[] args, Dictionary<string, object> context)
78+
{
79+
using (logger.BeginScope(context))
80+
{
81+
logger.LogInformation(messageTemplate, args);
82+
}
83+
}
84+
7785
public void LogMessageInNestedScopes()
7886
{
7987
using (var _ = logger.BeginScope("{ScopeKey1}", "scopeValue1"))

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/NLogLoggingAdapter.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ public void InfoWithStructuredArgs(string messageTemplate, object[] args)
7272
_log.Info(messageTemplate, args);
7373
}
7474

75+
public void InfoWithStructuredArgsAndContextDictionary(string messageTemplate, object[] args, Dictionary<string, object> context)
76+
{
77+
var logEvent = new LogEventInfo(LogLevel.Info, null, messageTemplate);
78+
logEvent.Parameters = args;
79+
foreach (var kvp in context)
80+
{
81+
logEvent.Properties[kvp.Key] = kvp.Value;
82+
}
83+
_log.Log(logEvent);
84+
}
85+
7586
public void LogMessageInNestedScopes()
7687
{
7788
throw new NotImplementedException();

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/SerilogExtensionsLoggingAdapter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public void InfoWithStructuredArgs(string messageTemplate, object[] args)
7474
_logger.LogInformation(messageTemplate, args);
7575
}
7676

77+
public void InfoWithStructuredArgsAndContextDictionary(string messageTemplate, object[] args, Dictionary<string, object> context)
78+
{
79+
using (_logger.BeginScope(context))
80+
{
81+
_logger.LogInformation(messageTemplate, args);
82+
}
83+
}
84+
7785
public void LogMessageInNestedScopes()
7886
{
7987
using (var _ = _logger.BeginScope("{ScopeKey1}", "scopeValue1"))

tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/LogInstrumentation/SerilogLoggingAdapter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ public void InfoWithStructuredArgs(string messageTemplate, object[] args)
7979
_log.Information(messageTemplate, args);
8080
}
8181

82+
public void InfoWithStructuredArgsAndContextDictionary(string messageTemplate, object[] args, Dictionary<string, object> context)
83+
{
84+
var loggerConfig = new LoggerConfiguration();
85+
86+
loggerConfig
87+
.MinimumLevel.Information()
88+
.Enrich.With(new ContextDataEnricher(context))
89+
.WriteTo.Console();
90+
91+
var logger = loggerConfig.CreateLogger();
92+
93+
logger.Information(messageTemplate, args);
94+
}
95+
8296
public void LogMessageInNestedScopes()
8397
{
8498
throw new NotImplementedException();

0 commit comments

Comments
 (0)