未验证 提交 ed2e7490 编写于 作者: D daveMueller 提交者: GitHub

JSON Console Logger not requiring scopes to implement IReadOnlyCollection (#48453)

上级 464208f5
......@@ -109,7 +109,7 @@ private void WriteScopeInformation(Utf8JsonWriter writer, IExternalScopeProvider
writer.WriteStartArray("Scopes");
scopeProvider.ForEachScope((scope, state) =>
{
if (scope is IReadOnlyCollection<KeyValuePair<string, object>> scopes)
if (scope is IEnumerable<KeyValuePair<string, object>> scopes)
{
state.WriteStartObject();
state.WriteString("Message", scope.ToString());
......
......@@ -342,6 +342,35 @@ public void Log_StateAndScopeContainsNullValue_SerializesNull()
Assert.Contains("\"LogKey\":null", message);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void Log_ScopeIsIEnumerable_SerializesKeyValuePair()
{
// Arrange
var t = SetUp(
new ConsoleLoggerOptions { FormatterName = ConsoleFormatterNames.Json },
simpleOptions: null,
systemdOptions: null,
jsonOptions: new JsonConsoleFormatterOptions
{
JsonWriterOptions = new JsonWriterOptions() { Indented = false },
IncludeScopes = true
}
);
var logger = (ILogger)t.Logger;
var sink = t.Sink;
// Act
using (logger.BeginScope(new[] { 2 }.Select(x => new KeyValuePair<string, object>("Value", x))))
{
logger.LogInformation("{LogEntryNumber}", 1);
}
// Assert
string message = sink.Writes[0].Message;
Assert.Contains("\"Message\":\"System.Linq.Enumerable", message);
Assert.Contains("\"Value\":" + 2, message);
}
public static TheoryData<object, string> SpecialCaseValues
{
get
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册