Information at endpoint and service-method boundaries, Debug in deeper helpers, Warning before expected/recoverable failures (not-found, validation, agent tool errors), Error on caught exceptions. Serilog wraps the exception handler so request-completion logs report the resolved status code rather than the raw exception. Never logs prose bodies or the Anthropic API key.
25 lines
1.0 KiB
C#
25 lines
1.0 KiB
C#
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Novelly.Api.Tests;
|
|
|
|
/// <summary>Records every entry logged through it, so tests can assert on what a service logged.</summary>
|
|
public record CapturedLogEntry(LogLevel Level, string Message, Exception? Exception);
|
|
|
|
/// <summary>
|
|
/// A test double for <see cref="ILogger{TCategoryName}"/> that captures entries instead of
|
|
/// writing them anywhere, so tests can assert a service logged at the right level with the
|
|
/// right values without standing up a real sink.
|
|
/// </summary>
|
|
public class CapturingLogger<T> : ILogger<T>
|
|
{
|
|
public List<CapturedLogEntry> Entries { get; } = [];
|
|
|
|
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) =>
|
|
Entries.Add(new CapturedLogEntry(logLevel, formatter(state, exception), exception));
|
|
}
|