Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit f7d8e4e

Browse files
author
John Luo
committed
Make LogLevelAttribute applicable to classes and assemblies
1 parent e94b64f commit f7d8e4e

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

src/Microsoft.Extensions.Logging.Testing/LoggedTest/LoggedTestBase.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public virtual void Initialize(MethodInfo methodInfo, object[] testMethodArgumen
5151
TestOutputHelper = testOutputHelper;
5252

5353
var classType = GetType();
54-
var logLevelAttribute = methodInfo.GetCustomAttribute<LogLevelAttribute>();
54+
var logLevelAttribute = methodInfo.GetCustomAttribute<LogLevelAttribute>()
55+
?? methodInfo.DeclaringType.GetCustomAttribute<LogLevelAttribute>()
56+
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<LogLevelAttribute>();
5557
var testName = testMethodArguments.Aggregate(methodInfo.Name, (a, b) => $"{a}-{(b ?? "null")}");
5658

5759
var useShortClassName = methodInfo.DeclaringType.GetCustomAttribute<ShortClassNameAttribute>()

src/Microsoft.Extensions.Logging.Testing/Xunit/LogLevelAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Microsoft.Extensions.Logging.Testing
77
{
8-
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
8+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = false)]
99
public class LogLevelAttribute : Attribute
1010
{
1111
public LogLevelAttribute(LogLevel logLevel)

test/Microsoft.Extensions.Logging.Testing.Tests/LoggedTestXunitTests.cs

+26-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Microsoft.Extensions.Logging.Testing.Tests
1212
{
13+
[LogLevel(LogLevel.Debug)]
1314
[ShortClassName]
1415
public class LoggedTestXunitTests : TestLoggedTest
1516
{
@@ -76,7 +77,7 @@ public void LoggedConditionalTheoryInitializesLoggedTestProperties(string argume
7677

7778
[Fact]
7879
[LogLevel(LogLevel.Information)]
79-
public void LoggedFactFilteredByLogLevel()
80+
public void LoggedFactFilteredByMethodLogLevel()
8081
{
8182
Logger.LogInformation("Information");
8283
Logger.LogDebug("Debug");
@@ -86,6 +87,17 @@ public void LoggedFactFilteredByLogLevel()
8687
Assert.Equal("Information", message.Formatter(message.State, null));
8788
}
8889

90+
[Fact]
91+
public void LoggedFactFilteredByClassLogLevel()
92+
{
93+
Logger.LogDebug("Debug");
94+
Logger.LogTrace("Trace");
95+
96+
var message = Assert.Single(TestSink.Writes);
97+
Assert.Equal(LogLevel.Debug, message.LogLevel);
98+
Assert.Equal("Debug", message.Formatter(message.State, null));
99+
}
100+
89101
[Theory]
90102
[InlineData("Hello world")]
91103
[LogLevel(LogLevel.Information)]
@@ -150,6 +162,19 @@ public void ScopeStartedEventInvoked()
150162
}
151163
}
152164

165+
public class LoggedTestXunitLogLevelTests : LoggedTest
166+
{
167+
[Fact]
168+
public void LoggedFactFilteredByAssemblyLogLevel()
169+
{
170+
Logger.LogTrace("Trace");
171+
172+
var message = Assert.Single(TestSink.Writes);
173+
Assert.Equal(LogLevel.Trace, message.LogLevel);
174+
Assert.Equal("Trace", message.Formatter(message.State, null));
175+
}
176+
}
177+
153178
public class LoggedTestXunitInitializationTests : TestLoggedTest
154179
{
155180
[Fact]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using Microsoft.Extensions.Logging;
2+
using Microsoft.Extensions.Logging.Testing;
3+
4+
[assembly: LogLevel(LogLevel.Trace)]

0 commit comments

Comments
 (0)