Make LogLevelAttribute applicable to classes and assemblies

This commit is contained in:
John Luo 2018-11-06 14:39:40 -08:00
Родитель e94b64f96b
Коммит f7d8e4e053
4 изменённых файлов: 34 добавлений и 3 удалений

Просмотреть файл

@ -51,7 +51,9 @@ namespace Microsoft.Extensions.Logging.Testing
TestOutputHelper = testOutputHelper;
var classType = GetType();
var logLevelAttribute = methodInfo.GetCustomAttribute<LogLevelAttribute>();
var logLevelAttribute = methodInfo.GetCustomAttribute<LogLevelAttribute>()
?? methodInfo.DeclaringType.GetCustomAttribute<LogLevelAttribute>()
?? methodInfo.DeclaringType.Assembly.GetCustomAttribute<LogLevelAttribute>();
var testName = testMethodArguments.Aggregate(methodInfo.Name, (a, b) => $"{a}-{(b ?? "null")}");
var useShortClassName = methodInfo.DeclaringType.GetCustomAttribute<ShortClassNameAttribute>()

Просмотреть файл

@ -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)

Просмотреть файл

@ -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]

Просмотреть файл

@ -0,0 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
[assembly: LogLevel(LogLevel.Trace)]