-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathILoggerBuilderExtensions.cs
More file actions
72 lines (65 loc) · 3.01 KB
/
Copy pathILoggerBuilderExtensions.cs
File metadata and controls
72 lines (65 loc) · 3.01 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
using System;
using Arcus.Testing;
using ITUnitLogger = TUnit.Core.Logging.ILogger;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.Logging
{
/// <summary>
/// Extensions on the <see cref="ILoggingBuilder"/> related to logging.
/// </summary>
// ReSharper disable once InconsistentNaming
public static class ILoggerBuilderExtensions
{
/// <summary>
/// Adds the logging messages from the given TUnit <paramref name="outputWriter"/> as a provider to the <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The logging builder to add the NUnit logging test messages to.</param>
/// <param name="outputWriter">The TUnit test writer to write custom test output.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or the <paramref name="outputWriter"/> is <c>null</c>.</exception>
public static ILoggingBuilder AddTUnitTestLogging(this ILoggingBuilder builder, ITUnitLogger outputWriter)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(outputWriter);
#pragma warning disable CA2000 // Responsibility of disposing the created object is transferred to the caller
var provider = new TUnitLoggerProvider(outputWriter);
#pragma warning restore CA2000
return builder.AddProvider(provider);
}
[ProviderAlias("TUnit")]
private sealed class TUnitLoggerProvider : ILoggerProvider, ISupportExternalScope
{
private readonly ITUnitLogger _testLogger;
private IExternalScopeProvider _scopeProvider;
/// <summary>
/// Initializes a new instance of the <see cref="TUnitLoggerProvider"/> class.
/// </summary>
internal TUnitLoggerProvider(ITUnitLogger testLogger)
{
_testLogger = testLogger;
}
/// <summary>
/// Creates a new <see cref="ILogger" /> instance.
/// </summary>
/// <param name="categoryName">The category name for messages produced by the logger.</param>
/// <returns>The instance of <see cref="ILogger" /> that was created.</returns>
public ILogger CreateLogger(string categoryName)
{
return new TUnitTestLogger(_testLogger, _scopeProvider, categoryName);
}
/// <summary>
/// Sets external scope information source for logger provider.
/// </summary>
/// <param name="scopeProvider">The provider of scope data.</param>
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
{
_scopeProvider = scopeProvider;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
}
}
}
}