-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
LoggerFactoryExtensionsTest.cs
196 lines (161 loc) · 7.8 KB
/
LoggerFactoryExtensionsTest.cs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using Microsoft.Extensions.Logging.Testing;
using Moq;
using Xunit;
namespace Microsoft.Extensions.Logging.Test
{
public class LoggerFactoryExtensionsTest
{
// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34091", TestRuntimes.Mono)]
public void LoggerFactoryCreateOfT_CallsCreateWithCorrectName()
{
// Arrange
var expected = typeof(TestType).FullName;
var factory = new Mock<ILoggerFactory>();
factory.Setup(f => f.CreateLogger(
It.IsAny<string>()))
.Returns(new Mock<ILogger>().Object);
// Act
factory.Object.CreateLogger<TestType>();
// Assert
factory.Verify(f => f.CreateLogger(expected));
}
// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34091", TestRuntimes.Mono)]
public void LoggerFactoryCreateOfT_SingleGeneric_CallsCreateWithCorrectName()
{
// Arrange
var factory = new Mock<ILoggerFactory>();
factory.Setup(f => f.CreateLogger(It.Is<string>(
x => x.Equals("Microsoft.Extensions.Logging.Test.GenericClass<Microsoft.Extensions.Logging.Test.TestType>"))))
.Returns(new Mock<ILogger>().Object);
var logger = factory.Object.CreateLogger<GenericClass<TestType>>();
// Assert
Assert.NotNull(logger);
}
// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34091", TestRuntimes.Mono)]
public void LoggerFactoryCreateOfT_TwoGenerics_CallsCreateWithCorrectName()
{
// Arrange
var factory = new Mock<ILoggerFactory>();
factory.Setup(f => f.CreateLogger(It.Is<string>(
x => x.Equals("Microsoft.Extensions.Logging.Test.GenericClass<Microsoft.Extensions.Logging.Test.TestType, Microsoft.Extensions.Logging.Test.SecondTestType>"))))
.Returns(new Mock<ILogger>().Object);
var logger = factory.Object.CreateLogger<GenericClass<TestType, SecondTestType>>();
// Assert
Assert.NotNull(logger);
}
[Fact]
public void CreatesLoggerName_WithoutGenericTypeArgumentsInformation()
{
// Arrange
var fullName = typeof(GenericClass<string>).GetGenericTypeDefinition().FullName;
var fullNameWithoutBacktick = fullName.Substring(0, fullName.IndexOf('`'));
var testSink = new TestSink();
var factory = new TestLoggerFactory(testSink, enabled: true);
// Act
var logger = factory.CreateLogger<GenericClass<string>>();
logger.LogInformation("test message");
// Assert
var sinkWrite = Assert.Single(testSink.Writes);
Assert.Equal(fullNameWithoutBacktick, sinkWrite.LoggerName);
}
[Fact]
public void CreatesLoggerName_OnNestedGenericType_CreatesWithoutGenericTypeArgumentsInformation()
{
// Arrange
var fullName = typeof(GenericClass<GenericClass<string>>).GetGenericTypeDefinition().FullName;
var fullNameWithoutBacktick = fullName.Substring(0, fullName.IndexOf('`'));
var testSink = new TestSink();
var factory = new TestLoggerFactory(testSink, enabled: true);
// Act
var logger = factory.CreateLogger<GenericClass<GenericClass<string>>>();
logger.LogInformation("test message");
// Assert
var sinkWrite = Assert.Single(testSink.Writes);
Assert.Equal(fullNameWithoutBacktick, sinkWrite.LoggerName);
}
[Fact]
public void CreatesLoggerName_OnMultipleTypeArgumentGenericType_CreatesWithoutGenericTypeArgumentsInformation()
{
// Arrange
var fullName = typeof(GenericClass<string, string>).GetGenericTypeDefinition().FullName;
var fullNameWithoutBacktick = fullName.Substring(0, fullName.IndexOf('`'));
var testSink = new TestSink();
var factory = new TestLoggerFactory(testSink, enabled: true);
// Act
var logger = factory.CreateLogger<GenericClass<string, string>>();
logger.LogInformation("test message");
// Assert
var sinkWrite = Assert.Single(testSink.Writes);
Assert.Equal(fullNameWithoutBacktick, sinkWrite.LoggerName);
}
// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34091", TestRuntimes.Mono)]
public void LoggerFactoryCreate_CallsCreateWithCorrectName()
{
// Arrange
var expected = typeof(TestType).FullName;
var factory = new Mock<ILoggerFactory>();
factory.Setup(f => f.CreateLogger(
It.IsAny<string>()))
.Returns(new Mock<ILogger>().Object);
// Act
factory.Object.CreateLogger(typeof(TestType));
// Assert
factory.Verify(f => f.CreateLogger(expected));
}
// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34091", TestRuntimes.Mono)]
public void LoggerFactoryCreate_SingleGeneric_CallsCreateWithCorrectName()
{
// Arrange
var factory = new Mock<ILoggerFactory>();
factory.Setup(f => f.CreateLogger(It.Is<string>(
x => x.Equals("Microsoft.Extensions.Logging.Test.GenericClass"))))
.Returns(new Mock<ILogger>().Object);
var logger = factory.Object.CreateLogger(typeof(GenericClass<TestType>));
// Assert
Assert.NotNull(logger);
}
// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34091", TestRuntimes.Mono)]
public void LoggerFactoryCreate_TwoGenerics_CallsCreateWithCorrectName()
{
// Arrange
var factory = new Mock<ILoggerFactory>();
factory.Setup(f => f.CreateLogger(It.Is<string>(
x => x.Equals("Microsoft.Extensions.Logging.Test.GenericClass"))))
.Returns(new Mock<ILogger>().Object);
var logger = factory.Object.CreateLogger(typeof(GenericClass<TestType, SecondTestType>));
// Assert
Assert.NotNull(logger);
}
}
internal class TestType
{
// intentionally holds nothing
}
internal class SecondTestType
{
// intentionally holds nothing
}
internal class GenericClass<X, Y> where X : class where Y : class
{
// intentionally holds nothing
}
internal class GenericClass<X> where X : class
{
// intentionally holds nothing
}
}