From f7d8e4e0537eaab54dcf28c2b148b82688a3d62d Mon Sep 17 00:00:00 2001 From: John Luo Date: Tue, 6 Nov 2018 14:39:40 -0800 Subject: [PATCH] Make LogLevelAttribute applicable to classes and assemblies --- .../LoggedTest/LoggedTestBase.cs | 4 ++- .../Xunit/LogLevelAttribute.cs | 2 +- .../LoggedTestXunitTests.cs | 27 ++++++++++++++++++- .../Properties/AssemlyInfo.cs | 4 +++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/Microsoft.Extensions.Logging.Testing.Tests/Properties/AssemlyInfo.cs diff --git a/src/Microsoft.Extensions.Logging.Testing/LoggedTest/LoggedTestBase.cs b/src/Microsoft.Extensions.Logging.Testing/LoggedTest/LoggedTestBase.cs index f714a63..c3a4e78 100644 --- a/src/Microsoft.Extensions.Logging.Testing/LoggedTest/LoggedTestBase.cs +++ b/src/Microsoft.Extensions.Logging.Testing/LoggedTest/LoggedTestBase.cs @@ -51,7 +51,9 @@ namespace Microsoft.Extensions.Logging.Testing TestOutputHelper = testOutputHelper; var classType = GetType(); - var logLevelAttribute = methodInfo.GetCustomAttribute(); + var logLevelAttribute = methodInfo.GetCustomAttribute() + ?? methodInfo.DeclaringType.GetCustomAttribute() + ?? methodInfo.DeclaringType.Assembly.GetCustomAttribute(); var testName = testMethodArguments.Aggregate(methodInfo.Name, (a, b) => $"{a}-{(b ?? "null")}"); var useShortClassName = methodInfo.DeclaringType.GetCustomAttribute() diff --git a/src/Microsoft.Extensions.Logging.Testing/Xunit/LogLevelAttribute.cs b/src/Microsoft.Extensions.Logging.Testing/Xunit/LogLevelAttribute.cs index 9f6f621..5de51cf 100644 --- a/src/Microsoft.Extensions.Logging.Testing/Xunit/LogLevelAttribute.cs +++ b/src/Microsoft.Extensions.Logging.Testing/Xunit/LogLevelAttribute.cs @@ -5,7 +5,7 @@ using System; namespace Microsoft.Extensions.Logging.Testing { - [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = false)] public class LogLevelAttribute : Attribute { public LogLevelAttribute(LogLevel logLevel) diff --git a/test/Microsoft.Extensions.Logging.Testing.Tests/LoggedTestXunitTests.cs b/test/Microsoft.Extensions.Logging.Testing.Tests/LoggedTestXunitTests.cs index 4b216c3..507453a 100644 --- a/test/Microsoft.Extensions.Logging.Testing.Tests/LoggedTestXunitTests.cs +++ b/test/Microsoft.Extensions.Logging.Testing.Tests/LoggedTestXunitTests.cs @@ -10,6 +10,7 @@ using Xunit.Abstractions; namespace Microsoft.Extensions.Logging.Testing.Tests { + [LogLevel(LogLevel.Debug)] [ShortClassName] public class LoggedTestXunitTests : TestLoggedTest { @@ -76,7 +77,7 @@ namespace Microsoft.Extensions.Logging.Testing.Tests [Fact] [LogLevel(LogLevel.Information)] - public void LoggedFactFilteredByLogLevel() + public void LoggedFactFilteredByMethodLogLevel() { Logger.LogInformation("Information"); Logger.LogDebug("Debug"); @@ -86,6 +87,17 @@ namespace Microsoft.Extensions.Logging.Testing.Tests Assert.Equal("Information", message.Formatter(message.State, null)); } + [Fact] + public void LoggedFactFilteredByClassLogLevel() + { + Logger.LogDebug("Debug"); + Logger.LogTrace("Trace"); + + var message = Assert.Single(TestSink.Writes); + Assert.Equal(LogLevel.Debug, message.LogLevel); + Assert.Equal("Debug", message.Formatter(message.State, null)); + } + [Theory] [InlineData("Hello world")] [LogLevel(LogLevel.Information)] @@ -150,6 +162,19 @@ namespace Microsoft.Extensions.Logging.Testing.Tests } } + public class LoggedTestXunitLogLevelTests : LoggedTest + { + [Fact] + public void LoggedFactFilteredByAssemblyLogLevel() + { + Logger.LogTrace("Trace"); + + var message = Assert.Single(TestSink.Writes); + Assert.Equal(LogLevel.Trace, message.LogLevel); + Assert.Equal("Trace", message.Formatter(message.State, null)); + } + } + public class LoggedTestXunitInitializationTests : TestLoggedTest { [Fact] diff --git a/test/Microsoft.Extensions.Logging.Testing.Tests/Properties/AssemlyInfo.cs b/test/Microsoft.Extensions.Logging.Testing.Tests/Properties/AssemlyInfo.cs new file mode 100644 index 0000000..63c2b71 --- /dev/null +++ b/test/Microsoft.Extensions.Logging.Testing.Tests/Properties/AssemlyInfo.cs @@ -0,0 +1,4 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Testing; + +[assembly: LogLevel(LogLevel.Trace)]