// Copyright (c) .NET Foundation. All rights reserved. // See License.txt in the project root for license information using System; using System.Linq; using Microsoft.Extensions.Logging; using Xunit.Abstractions; namespace Microsoft.AspNetCore.Testing { public class XunitLogger : ILogger, IDisposable { private readonly ITestOutputHelper _output; private readonly string _categoryName; public XunitLogger(ITestOutputHelper output, string categoryName) {; _output = output; _categoryName = categoryName; } public void Log( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { var firstLinePrefix = $"| {logLevel} [{_categoryName}]: "; var lines = formatter(state, exception).Split('\n'); _output.WriteLine(firstLinePrefix + lines.First()); var additionalLinePrefix = "|" + new string(' ', firstLinePrefix.Length - 1); foreach (var line in lines.Skip(1)) { _output.WriteLine(additionalLinePrefix + line.Trim('\r')); } } public bool IsEnabled(LogLevel logLevel) => true; public IDisposable BeginScope(TState state) => new NullScope(); private class NullScope : IDisposable { public void Dispose() { } } public void Dispose() { } } }