-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from 0xced/IMessageSink
Add support for IMessageSink
- Loading branch information
Showing
22 changed files
with
985 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Martin Costello, 2018. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace MartinCostello.Logging.XUnit | ||
{ | ||
/// <summary> | ||
/// Defines a property for accessing an <see cref="IMessageSink"/>. | ||
/// </summary> | ||
public interface IMessageSinkAccessor | ||
{ | ||
/// <summary> | ||
/// Gets or sets the <see cref="IMessageSink"/> to use. | ||
/// </summary> | ||
IMessageSink? MessageSink { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Martin Costello, 2018. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Xunit.Abstractions | ||
{ | ||
/// <summary> | ||
/// A class containing extension methods for the <see cref="IMessageSink"/> interface. This class cannot be inherited. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class IMessageSinkExtensions | ||
{ | ||
/// <summary> | ||
/// Returns an <see cref="ILoggerFactory"/> that logs to the message sink. | ||
/// </summary> | ||
/// <param name="messageSink">The <see cref="IMessageSink"/> to create the logger factory from.</param> | ||
/// <returns> | ||
/// An <see cref="ILoggerFactory"/> that writes messages to the message sink. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="messageSink"/> is <see langword="null"/>. | ||
/// </exception> | ||
public static ILoggerFactory ToLoggerFactory(this IMessageSink messageSink) | ||
{ | ||
if (messageSink == null) | ||
{ | ||
throw new ArgumentNullException(nameof(messageSink)); | ||
} | ||
|
||
return new LoggerFactory().AddXUnit(messageSink); | ||
} | ||
|
||
/// <summary> | ||
/// Returns an <see cref="ILogger{T}"/> that logs to the message sink. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the logger to create.</typeparam> | ||
/// <param name="messageSink">The <see cref="IMessageSink"/> to create the logger from.</param> | ||
/// <returns> | ||
/// An <see cref="ILogger{T}"/> that writes messages to the message sink. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="messageSink"/> is <see langword="null"/>. | ||
/// </exception> | ||
public static ILogger<T> ToLogger<T>(this IMessageSink messageSink) | ||
=> messageSink.ToLoggerFactory().CreateLogger<T>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Martin Costello, 2018. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Xunit.Abstractions; | ||
|
||
namespace MartinCostello.Logging.XUnit | ||
{ | ||
/// <summary> | ||
/// A class representing the default implementation of <see cref="IMessageSinkAccessor"/>. This class cannot be inherited. | ||
/// </summary> | ||
internal sealed class MessageSinkAccessor : IMessageSinkAccessor | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MessageSinkAccessor"/> class. | ||
/// </summary> | ||
/// <param name="messageSink">The <see cref="IMessageSink"/> to use.</param> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="messageSink"/> is <see langword="null"/>. | ||
/// </exception> | ||
internal MessageSinkAccessor(IMessageSink messageSink) | ||
{ | ||
MessageSink = messageSink ?? throw new ArgumentNullException(nameof(messageSink)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the current <see cref="IMessageSink"/>. | ||
/// </summary> | ||
public IMessageSink? MessageSink { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Martin Costello, 2018. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit.Abstractions; | ||
|
||
namespace MartinCostello.Logging.XUnit | ||
{ | ||
/// <summary> | ||
/// A class representing an <see cref="ILogger"/> to use with xunit. | ||
/// </summary> | ||
public partial class XUnitLogger | ||
{ | ||
/// <summary> | ||
/// The <see cref="IMessageSinkAccessor"/> to use. This field is read-only. | ||
/// </summary> | ||
private readonly IMessageSinkAccessor? _messageSinkAccessor; | ||
|
||
/// <summary> | ||
/// Gets or sets the message sink message factory to use when writing to an <see cref="IMessageSink"/>. | ||
/// </summary> | ||
private Func<string, IMessageSinkMessage> _messageSinkMessageFactory; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="XUnitLogger"/> class. | ||
/// </summary> | ||
/// <param name="name">The name for messages produced by the logger.</param> | ||
/// <param name="messageSink">The <see cref="IMessageSink"/> to use.</param> | ||
/// <param name="options">The <see cref="XUnitLoggerOptions"/> to use.</param> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="name"/> or <paramref name="messageSink"/> is <see langword="null"/>. | ||
/// </exception> | ||
public XUnitLogger(string name, IMessageSink messageSink, XUnitLoggerOptions? options) | ||
: this(name, new MessageSinkAccessor(messageSink), options) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="XUnitLogger"/> class. | ||
/// </summary> | ||
/// <param name="name">The name for messages produced by the logger.</param> | ||
/// <param name="accessor">The <see cref="IMessageSinkAccessor"/> to use.</param> | ||
/// <param name="options">The <see cref="XUnitLoggerOptions"/> to use.</param> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="name"/> or <paramref name="accessor"/> is <see langword="null"/>. | ||
/// </exception> | ||
public XUnitLogger(string name, IMessageSinkAccessor accessor, XUnitLoggerOptions? options) | ||
: this(name, options) | ||
{ | ||
_messageSinkAccessor = accessor ?? throw new ArgumentNullException(nameof(accessor)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the message sink message factory to use when writing to an <see cref="IMessageSink"/>. | ||
/// </summary> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="value"/> is <see langword="null"/>. | ||
/// </exception> | ||
public Func<string, IMessageSinkMessage> MessageSinkMessageFactory | ||
{ | ||
get { return _messageSinkMessageFactory; } | ||
set { _messageSinkMessageFactory = value ?? throw new ArgumentNullException(nameof(value)); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Martin Costello, 2018. All rights reserved. | ||
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit.Abstractions; | ||
|
||
namespace MartinCostello.Logging.XUnit | ||
{ | ||
/// <summary> | ||
/// A class representing an <see cref="ILogger"/> to use with xunit. | ||
/// </summary> | ||
public partial class XUnitLogger | ||
{ | ||
/// <summary> | ||
/// The <see cref="ITestOutputHelperAccessor"/> to use. This field is read-only. | ||
/// </summary> | ||
private readonly ITestOutputHelperAccessor? _outputHelperAccessor; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="XUnitLogger"/> class. | ||
/// </summary> | ||
/// <param name="name">The name for messages produced by the logger.</param> | ||
/// <param name="outputHelper">The <see cref="ITestOutputHelper"/> to use.</param> | ||
/// <param name="options">The <see cref="XUnitLoggerOptions"/> to use.</param> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="name"/> or <paramref name="outputHelper"/> is <see langword="null"/>. | ||
/// </exception> | ||
public XUnitLogger(string name, ITestOutputHelper outputHelper, XUnitLoggerOptions? options) | ||
: this(name, new TestOutputHelperAccessor(outputHelper), options) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="XUnitLogger"/> class. | ||
/// </summary> | ||
/// <param name="name">The name for messages produced by the logger.</param> | ||
/// <param name="accessor">The <see cref="ITestOutputHelperAccessor"/> to use.</param> | ||
/// <param name="options">The <see cref="XUnitLoggerOptions"/> to use.</param> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="name"/> or <paramref name="accessor"/> is <see langword="null"/>. | ||
/// </exception> | ||
public XUnitLogger(string name, ITestOutputHelperAccessor accessor, XUnitLoggerOptions? options) | ||
: this(name, options) | ||
{ | ||
_outputHelperAccessor = accessor ?? throw new ArgumentNullException(nameof(accessor)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.