Skip to content

Commit a548cea

Browse files
authored
Fixed issue when EventFlowLogger throws on duplicate log message arguments (#413)
* fixed issue when EventFlowLogger throws on duplicate log message arguments * PR feedback
1 parent 7dd42c7 commit a548cea

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/Microsoft.Diagnostics.EventFlow.Inputs.MicrosoftLogging/Logger/EventFlowLogger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void Log<TState>(Extensions.Logging.LogLevel logLevel, EventId eventId, T
4040

4141
Dictionary<string, object> properties = new Dictionary<string, object>();
4242

43-
ApplyOmittingUnformattedMessage(state, item => properties.Add(item.Key, item.Value));
43+
ApplyOmittingUnformattedMessage(state, item => properties[item.Key] = item.Value);
4444

4545
var scope = EventFlowLoggerScope.Current;
4646
if (scope != null)

src/Microsoft.Diagnostics.EventFlow.Inputs.MicrosoftLogging/Microsoft.Diagnostics.EventFlow.Inputs.MicrosoftLogging.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Authors>Microsoft</Authors>
77
<TargetFrameworks>netstandard1.6;netstandard2.0;net462</TargetFrameworks>
88
<AssemblyName>Microsoft.Diagnostics.EventFlow.Inputs.MicrosoftLogging</AssemblyName>
9-
<VersionPrefix>1.6.0</VersionPrefix>
9+
<VersionPrefix>1.6.1</VersionPrefix>
1010
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
1111
<PackageId>Microsoft.Diagnostics.EventFlow.Inputs.MicrosoftLogging</PackageId>
1212
<PackageTags>Microsoft;Diagnostics;EventFlow;Inputs;MicrosoftLogging</PackageTags>

test/Microsoft.Diagnostics.EventFlow.Inputs.Tests/LoggerInputTests.cs

+41
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@ public void LoggerShouldSubmitData()
4949
}
5050
}
5151

52+
[Fact]
53+
public void LoggerShouldSubmitDataWhenEncounterDuplicates()
54+
{
55+
var healthReporterMock = new Mock<IHealthReporter>();
56+
var subject = new Mock<IObserver<EventData>>();
57+
EventData savedData = null;
58+
subject.Setup(s => s.OnNext(It.IsAny<EventData>())).Callback((EventData data) => savedData = data);
59+
60+
using (LoggerInput target = new LoggerInput(healthReporterMock.Object))
61+
{
62+
var diagnosticPipeline = createPipeline(target, healthReporterMock.Object);
63+
using (target.Subscribe(subject.Object))
64+
{
65+
var factory = new LoggerFactory();
66+
factory.AddEventFlow(diagnosticPipeline);
67+
var logger = new Logger<LoggerInputTests>(factory);
68+
logger.LogInformation("log message {number} {number}", 1, 1);
69+
subject.Verify(s => s.OnNext(It.Is<EventData>(data => checkEventData(
70+
data,
71+
"log message 1 1",
72+
typeof(LoggerInputTests).FullName,
73+
LogLevel.Informational,
74+
0,
75+
null,
76+
new Dictionary<string, object> { { "number", 1 } }))), Times.Exactly(1));
77+
78+
assertDoesNotContainDuplicate(savedData.Payload, keyPrefix: "number", expectedValue: 1);
79+
80+
healthReporterMock.Verify(it => it.ReportWarning(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
81+
healthReporterMock.Verify(it => it.ReportProblem(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
82+
}
83+
}
84+
}
85+
5286
[Fact]
5387
public void LoggerShouldSubmitException()
5488
{
@@ -516,6 +550,13 @@ private void assertContainsDuplicate(IDictionary<string, object> payload, string
516550
Assert.Equal(expectedValue, payload[duplicates.First()]);
517551
}
518552

553+
private void assertDoesNotContainDuplicate(IDictionary<string, object> payload, string keyPrefix, object expectedValue)
554+
{
555+
var singleValue = payload.Keys.Where(k => k.StartsWith(keyPrefix)).ToArray();
556+
Assert.Single(singleValue);
557+
Assert.Equal(expectedValue, payload[singleValue.First()]);
558+
}
559+
519560
private DiagnosticPipeline createPipeline(LoggerInput input, IHealthReporter reporter)
520561
{
521562
return new DiagnosticPipeline(reporter, new[] { input }, null, new[] { new EventSink(new FakeOutput(), null) });

0 commit comments

Comments
 (0)