Skip to content

Commit 85f7a66

Browse files
committed
Allow setting default log levels in TestLoggerOptions
1 parent d0f7d13 commit 85f7a66

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

src/Foundatio.Xunit/Logging/TestLogger.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public TestLogger(Action<TestLoggerOptions> configure = null)
1717
{
1818
Options = new TestLoggerOptions();
1919
configure?.Invoke(Options);
20+
21+
foreach (var logLevel in Options.LogLevels)
22+
_logLevels[logLevel.Key] = logLevel.Value;
2023
}
2124

2225
public TestLogger(ITestOutputHelper output, Action<TestLoggerOptions> configure = null)
@@ -30,26 +33,26 @@ public TestLogger(ITestOutputHelper output, Action<TestLoggerOptions> configure
3033
};
3134

3235
configure?.Invoke(Options);
36+
37+
foreach (var logLevel in Options.LogLevels)
38+
_logLevels[logLevel.Key] = logLevel.Value;
3339
}
3440

3541
public TestLogger(TestLoggerOptions options)
3642
{
3743
Options = options ?? new TestLoggerOptions();
38-
}
3944

40-
public TestLoggerOptions Options { get; }
45+
foreach (var logLevel in Options.LogLevels)
46+
_logLevels[logLevel.Key] = logLevel.Value;
4147

42-
[Obsolete("Use DefaultMinimumLevel instead.")]
43-
public LogLevel MinimumLevel
44-
{
45-
get => Options.DefaultMinimumLevel;
46-
set => Options.DefaultMinimumLevel = value;
4748
}
4849

50+
public TestLoggerOptions Options { get; }
51+
4952
public LogLevel DefaultMinimumLevel
5053
{
51-
get => Options.DefaultMinimumLevel;
52-
set => Options.DefaultMinimumLevel = value;
54+
get => Options.DefaultLogLevel;
55+
set => Options.DefaultLogLevel = value;
5356
}
5457

5558
public int MaxLogEntriesToStore
@@ -66,13 +69,16 @@ public int MaxLogEntriesToWrite
6669

6770
public IReadOnlyList<LogEntry> LogEntries => _logEntries.ToArray();
6871

69-
7072
public void Reset()
7173
{
7274
lock (_logEntries)
7375
{
7476
_logEntries.Clear();
7577
_logLevels.Clear();
78+
79+
foreach (var logLevel in Options.LogLevels)
80+
_logLevels[logLevel.Key] = logLevel.Value;
81+
7682
Interlocked.Exchange(ref _logEntriesWritten, 0);
7783
}
7884
}
@@ -113,7 +119,7 @@ public bool IsEnabled(string category, LogLevel logLevel)
113119
if (_logLevels.TryGetValue(category, out var categoryLevel))
114120
return logLevel >= categoryLevel;
115121

116-
return logLevel >= Options.DefaultMinimumLevel;
122+
return logLevel >= Options.DefaultLogLevel;
117123
}
118124

119125
public void SetLogLevel(string category, LogLevel minLogLevel)

src/Foundatio.Xunit/Logging/TestLoggerLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
2424
if (!_logger.IsEnabled(_categoryName, logLevel))
2525
return;
2626

27-
object[] scopes = _logger.Options.IncludeScopes ? CurrentScopeStack.Reverse().ToArray() : Array.Empty<object>();
27+
object[] scopes = _logger.Options.IncludeScopes ? CurrentScopeStack.Reverse().ToArray() : [];
2828
var logEntry = new LogEntry
2929
{
3030
Date = _logger.Options.GetNow(),

src/Foundatio.Xunit/Logging/TestLoggerOptions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
using System;
2+
using System.Collections.Generic;
3+
using Foundatio.Utility;
24
using Microsoft.Extensions.Logging;
35
using Xunit.Abstractions;
46

57
namespace Foundatio.Xunit;
68

79
public class TestLoggerOptions
810
{
9-
public LogLevel DefaultMinimumLevel { get; set; } = LogLevel.Information;
11+
public LogLevel DefaultLogLevel { get; set; } = LogLevel.Information;
12+
public Dictionary<string, LogLevel> LogLevels { get; } = new();
1013
public int MaxLogEntriesToStore { get; set; } = 100;
1114
public int MaxLogEntriesToWrite { get; set; } = 1000;
1215
public bool IncludeScopes { get; set; } = true;
1316
public TimeProvider TimeProvider { get; set; } = TimeProvider.System;
1417

18+
public void SetLogLevel(string category, LogLevel minLogLevel)
19+
{
20+
LogLevels[category] = minLogLevel;
21+
}
22+
23+
public void SetLogLevel<T>(LogLevel minLogLevel)
24+
{
25+
SetLogLevel(TypeHelper.GetTypeDisplayName(typeof(T)), minLogLevel);
26+
}
27+
1528
public void UseOutputHelper(Func<ITestOutputHelper> getOutputHelper, Func<LogEntry, string> formatLogEntry = null)
1629
{
1730
formatLogEntry ??= logEntry => logEntry.ToString(false);

tests/Foundatio.Tests/Utility/TestLoggerTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public TestLoggerTests(ITestOutputHelper output, TestLoggerFixture fixture) : ba
2222
public void CanUseTestLogger()
2323
{
2424
var services = new ServiceCollection()
25-
.AddLogging(b => b.AddDebug().AddTestLogger(_output))
25+
.AddLogging(b => b.AddDebug().AddTestLogger(_output, o =>
26+
{
27+
o.SetLogLevel("Microsoft", LogLevel.Warning);
28+
}))
2629
.AddSingleton<SomeClass>();
2730

2831
IServiceProvider provider = services.BuildServiceProvider();

0 commit comments

Comments
 (0)