Skip to content

Commit

Permalink
Fixed issue when EventFlowLogger throws on duplicate log message argu…
Browse files Browse the repository at this point in the history
…ments (#413)

* fixed issue when EventFlowLogger throws on duplicate log message arguments

* PR feedback
  • Loading branch information
n-sidorov authored Aug 17, 2022
1 parent 7dd42c7 commit a548cea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Log<TState>(Extensions.Logging.LogLevel logLevel, EventId eventId, T

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

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

var scope = EventFlowLoggerScope.Current;
if (scope != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>Microsoft</Authors>
<TargetFrameworks>netstandard1.6;netstandard2.0;net462</TargetFrameworks>
<AssemblyName>Microsoft.Diagnostics.EventFlow.Inputs.MicrosoftLogging</AssemblyName>
<VersionPrefix>1.6.0</VersionPrefix>
<VersionPrefix>1.6.1</VersionPrefix>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageId>Microsoft.Diagnostics.EventFlow.Inputs.MicrosoftLogging</PackageId>
<PackageTags>Microsoft;Diagnostics;EventFlow;Inputs;MicrosoftLogging</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ public void LoggerShouldSubmitData()
}
}

[Fact]
public void LoggerShouldSubmitDataWhenEncounterDuplicates()
{
var healthReporterMock = new Mock<IHealthReporter>();
var subject = new Mock<IObserver<EventData>>();
EventData savedData = null;
subject.Setup(s => s.OnNext(It.IsAny<EventData>())).Callback((EventData data) => savedData = data);

using (LoggerInput target = new LoggerInput(healthReporterMock.Object))
{
var diagnosticPipeline = createPipeline(target, healthReporterMock.Object);
using (target.Subscribe(subject.Object))
{
var factory = new LoggerFactory();
factory.AddEventFlow(diagnosticPipeline);
var logger = new Logger<LoggerInputTests>(factory);
logger.LogInformation("log message {number} {number}", 1, 1);
subject.Verify(s => s.OnNext(It.Is<EventData>(data => checkEventData(
data,
"log message 1 1",
typeof(LoggerInputTests).FullName,
LogLevel.Informational,
0,
null,
new Dictionary<string, object> { { "number", 1 } }))), Times.Exactly(1));

assertDoesNotContainDuplicate(savedData.Payload, keyPrefix: "number", expectedValue: 1);

healthReporterMock.Verify(it => it.ReportWarning(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
healthReporterMock.Verify(it => it.ReportProblem(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
}
}
}

[Fact]
public void LoggerShouldSubmitException()
{
Expand Down Expand Up @@ -516,6 +550,13 @@ private void assertContainsDuplicate(IDictionary<string, object> payload, string
Assert.Equal(expectedValue, payload[duplicates.First()]);
}

private void assertDoesNotContainDuplicate(IDictionary<string, object> payload, string keyPrefix, object expectedValue)
{
var singleValue = payload.Keys.Where(k => k.StartsWith(keyPrefix)).ToArray();
Assert.Single(singleValue);
Assert.Equal(expectedValue, payload[singleValue.First()]);
}

private DiagnosticPipeline createPipeline(LoggerInput input, IHealthReporter reporter)
{
return new DiagnosticPipeline(reporter, new[] { input }, null, new[] { new EventSink(new FakeOutput(), null) });
Expand Down

0 comments on commit a548cea

Please sign in to comment.