-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogOptionsTests.cs
More file actions
140 lines (111 loc) · 4.02 KB
/
LogOptionsTests.cs
File metadata and controls
140 lines (111 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace ptr727.LanguageTags.Tests;
[Collection("Sequential Test Collection")]
public sealed class LogOptionsTests : SingleInstanceFixture
{
[Fact]
public void CreateLogger_UsesFactory_WhenFactorySet()
{
ILoggerFactory originalFactory = LogOptions.LoggerFactory;
using TestLoggerFactory testFactory = new();
try
{
LogOptions.LoggerFactory = testFactory;
ILogger logger = LogOptions.CreateLogger("category");
_ = LogOptions.LoggerFactory.Should().BeSameAs(testFactory);
_ = testFactory.LastCategory.Should().Be("category");
_ = logger.Should().NotBeNull();
}
finally
{
LogOptions.LoggerFactory = originalFactory;
}
}
[Fact]
public void CreateLogger_UsesNullLogger_WhenFactoryDefault()
{
ILoggerFactory originalFactory = LogOptions.LoggerFactory;
try
{
LogOptions.LoggerFactory = NullLoggerFactory.Instance;
ILogger logger = LogOptions.CreateLogger("category");
_ = logger.Should().BeSameAs(NullLogger.Instance);
}
finally
{
LogOptions.LoggerFactory = originalFactory;
}
}
[Fact]
public void CreateLogger_WithNullCategory_ThrowsArgumentNullException() =>
_ = Assert.Throws<ArgumentNullException>(() => LogOptions.CreateLogger(null!));
[Fact]
public void CreateLogger_WithEmptyCategory_ThrowsArgumentException() =>
_ = Assert.Throws<ArgumentException>(() => LogOptions.CreateLogger(" "));
[Fact]
public void TrySetFactory_WhenUnset_ReturnsTrueAndSets()
{
ILoggerFactory originalFactory = LogOptions.LoggerFactory;
using TestLoggerFactory testFactory = new();
try
{
LogOptions.LoggerFactory = NullLoggerFactory.Instance;
bool result = LogOptions.TrySetFactory(testFactory);
_ = result.Should().BeTrue();
_ = LogOptions.LoggerFactory.Should().BeSameAs(testFactory);
}
finally
{
LogOptions.LoggerFactory = originalFactory;
}
}
[Fact]
public void TrySetFactory_WhenAlreadySet_ReturnsFalseAndDoesNotOverwrite()
{
ILoggerFactory originalFactory = LogOptions.LoggerFactory;
using TestLoggerFactory testFactory = new();
using TestLoggerFactory otherFactory = new();
try
{
LogOptions.LoggerFactory = testFactory;
bool result = LogOptions.TrySetFactory(otherFactory);
_ = result.Should().BeFalse();
_ = LogOptions.LoggerFactory.Should().BeSameAs(testFactory);
}
finally
{
LogOptions.LoggerFactory = originalFactory;
}
}
private sealed class TestLoggerFactory : ILoggerFactory
{
public ILogger Logger { get; } = new TestLogger();
public string? LastCategory { get; private set; }
public void AddProvider(ILoggerProvider provider) { }
public ILogger CreateLogger(string categoryName)
{
LastCategory = categoryName;
return Logger;
}
public void Dispose() { }
}
private sealed class TestLogger : ILogger
{
public IDisposable BeginScope<TState>(TState state)
where TState : notnull => NullScope.Instance;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter
) { }
private sealed class NullScope : IDisposable
{
public static readonly NullScope Instance = new();
public void Dispose() { }
}
}
}