22// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
33
44using System ;
5- using System . Collections ;
65using System . Collections . Generic ;
7- using System . Linq ;
86using System . Text ;
97using Microsoft . Extensions . Logging ;
108using Xunit . Abstractions ;
9+ using Xunit . Sdk ;
1110
1211namespace MartinCostello . Logging . XUnit
1312{
1413 /// <summary>
1514 /// A class representing an <see cref="ILogger"/> to use with xunit.
1615 /// </summary>
17- public class XUnitLogger : ILogger
16+ public partial class XUnitLogger : ILogger
1817 {
1918 //// Based on https://github.com/aspnet/Logging/blob/master/src/Microsoft.Extensions.Logging.Console/ConsoleLogger.cs
2019
@@ -39,11 +38,6 @@ public class XUnitLogger : ILogger
3938 [ ThreadStatic ]
4039 private static StringBuilder ? _logBuilder ;
4140
42- /// <summary>
43- /// The <see cref="ITestOutputHelperAccessor"/> to use. This field is read-only.
44- /// </summary>
45- private readonly ITestOutputHelperAccessor _accessor ;
46-
4741 /// <summary>
4842 /// Gets or sets the filter to use.
4943 /// </summary>
@@ -53,31 +47,13 @@ public class XUnitLogger : ILogger
5347 /// Initializes a new instance of the <see cref="XUnitLogger"/> class.
5448 /// </summary>
5549 /// <param name="name">The name for messages produced by the logger.</param>
56- /// <param name="outputHelper">The <see cref="ITestOutputHelper"/> to use.</param>
57- /// <param name="options">The <see cref="XUnitLoggerOptions"/> to use.</param>
58- /// <exception cref="ArgumentNullException">
59- /// <paramref name="name"/> or <paramref name="outputHelper"/> is <see langword="null"/>.
60- /// </exception>
61- public XUnitLogger ( string name , ITestOutputHelper outputHelper , XUnitLoggerOptions ? options )
62- : this ( name , new TestOutputHelperAccessor ( outputHelper ) , options )
63- {
64- }
65-
66- /// <summary>
67- /// Initializes a new instance of the <see cref="XUnitLogger"/> class.
68- /// </summary>
69- /// <param name="name">The name for messages produced by the logger.</param>
70- /// <param name="accessor">The <see cref="ITestOutputHelperAccessor"/> to use.</param>
7150 /// <param name="options">The <see cref="XUnitLoggerOptions"/> to use.</param>
72- /// <exception cref="ArgumentNullException">
73- /// <paramref name="name"/> or <paramref name="accessor"/> is <see langword="null"/>.
74- /// </exception>
75- public XUnitLogger ( string name , ITestOutputHelperAccessor accessor , XUnitLoggerOptions ? options )
51+ private XUnitLogger ( string name , XUnitLoggerOptions ? options )
7652 {
7753 Name = name ?? throw new ArgumentNullException ( nameof ( name ) ) ;
78- _accessor = accessor ?? throw new ArgumentNullException ( nameof ( accessor ) ) ;
7954
80- _filter = options ? . Filter ?? ( ( category , logLevel ) => true ) ;
55+ _filter = options ? . Filter ?? ( ( _ , _ ) => true ) ;
56+ _messageSinkMessageFactory = options ? . MessageSinkMessageFactory ?? ( message => new DiagnosticMessage ( message ) ) ;
8157 IncludeScopes = options ? . IncludeScopes ?? false ;
8258 }
8359
@@ -152,17 +128,18 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState? state, Excep
152128 }
153129
154130 /// <summary>
155- /// Writes a message to the <see cref="ITestOutputHelper"/> associated with the instance.
131+ /// Writes a message to the <see cref="ITestOutputHelper"/> or <see cref="IMessageSink"/> associated with the instance.
156132 /// </summary>
157133 /// <param name="logLevel">The message to write will be written on this level.</param>
158134 /// <param name="eventId">The Id of the event.</param>
159135 /// <param name="message">The message to write.</param>
160136 /// <param name="exception">The exception related to this message.</param>
161137 public virtual void WriteMessage ( LogLevel logLevel , int eventId , string ? message , Exception ? exception )
162138 {
163- ITestOutputHelper ? outputHelper = _accessor . OutputHelper ;
139+ ITestOutputHelper ? outputHelper = _outputHelperAccessor ? . OutputHelper ;
140+ IMessageSink ? messageSink = _messageSinkAccessor ? . MessageSink ;
164141
165- if ( outputHelper == null )
142+ if ( outputHelper is null && messageSink is null )
166143 {
167144 return ;
168145 }
@@ -213,7 +190,17 @@ public virtual void WriteMessage(LogLevel logLevel, int eventId, string? message
213190
214191 try
215192 {
216- outputHelper . WriteLine ( $ "[{ Clock ( ) : u} ] { logLevelString } { formatted } ") ;
193+ var line = $ "[{ Clock ( ) : u} ] { logLevelString } { formatted } ";
194+ if ( outputHelper != null )
195+ {
196+ outputHelper . WriteLine ( line ) ;
197+ }
198+
199+ if ( messageSink != null )
200+ {
201+ var sinkMessage = _messageSinkMessageFactory ( line ) ;
202+ messageSink . OnMessage ( sinkMessage ) ;
203+ }
217204 }
218205#pragma warning disable CA1031
219206 catch ( InvalidOperationException )
0 commit comments