| File: XunitLoggerProvider.cs | |
| Project: ..\..\..\test\ProjectTemplates\Microsoft.Agents.AI.ProjectTemplates.IntegrationTests\Microsoft.Agents.AI.ProjectTemplates.Tests.csproj (Microsoft.Agents.AI.ProjectTemplates.Tests) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using Microsoft.Extensions.Logging; using Xunit; namespace Microsoft.Shared.ProjectTemplates.Tests; internal sealed class XunitLoggerProvider : ILoggerProvider { private readonly ITestOutputHelper _output; public XunitLoggerProvider(ITestOutputHelper output) { _output = output; } public ILogger CreateLogger(string categoryName) => new XunitLogger(_output, categoryName); public void Dispose() { // Nothing to dispose. } private sealed class XunitLogger : ILogger { private readonly ITestOutputHelper _output; private readonly string _categoryName; public XunitLogger(ITestOutputHelper output, string categoryName) { _output = output; _categoryName = categoryName; } public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null; public bool IsEnabled(LogLevel logLevel) => true; public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) { _output.WriteLine($"[{logLevel}] {_categoryName}: {formatter(state, exception)}"); if (exception is not null) { _output.WriteLine(exception.ToString()); } } } }