Class FakeLoggingBuilderExtensions
- Namespace
- LogAssertions.TUnit
- Assembly
- LogAssertions.TUnit.dll
ILoggingBuilder extensions that wire a Microsoft.Extensions.Logging.Testing.FakeLogCollector into a logging
builder you already own - a host's ConfigureLogging, a
Create(Action<ILoggingBuilder>) callback, or any other - so the
capture composes alongside your own level, filters, and providers. Use these when the self-contained
CreateTeed(LogLevel) tuple does not fit because the capture
must live inside an existing builder, for example an ASP.NET Core test host.
public static class FakeLoggingBuilderExtensions
- Inheritance
-
FakeLoggingBuilderExtensions
- Inherited Members
Methods
AddFakeLogging(ILoggingBuilder, FakeLogCollector, LogLevel)
Adds a Microsoft.Extensions.Logging.Testing.FakeLoggerProvider backed by collector for assertions and
sets the builder's minimum level to minimumLevel, registering that level as the
collector's capture floor so the vacuous-HasNotLogged() guard applies. No record is teed to
the test output; use AddTeedFakeLogging(ILoggingBuilder, FakeLogCollector, LogLevel, ILoggerProvider?) for that.
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The provider is registered into the ILoggingBuilder and becomes part of the built logger pipeline; its lifetime is the caller's, not this method's. FakeLoggerProvider holds no unmanaged resources.")]
public static ILoggingBuilder AddFakeLogging(this ILoggingBuilder builder, FakeLogCollector collector, LogLevel minimumLevel = LogLevel.Trace)
Parameters
builderILoggingBuilderThe logging builder to add the capture provider to.
collectorFakeLogCollectorThe collector that receives records for assertions.
minimumLevelLogLevelThe minimum level to capture, also registered as the capture floor. Default is Trace (everything).
Returns
- ILoggingBuilder
The same
builderfor chaining.
Remarks
The helper owns the builder's minimum level so the registered capture floor is accurate: a floor
below the level the builder actually filters at would let the vacuity guard miss a genuinely vacuous
absence and pass when it should fail. Pass a higher minimumLevel to capture less.
Exceptions
- ArgumentNullException
builderorcollectoris null.
AddTeedFakeLogging(ILoggingBuilder, FakeLogCollector, LogLevel, ILoggerProvider?)
Adds a Microsoft.Extensions.Logging.Testing.FakeLoggerProvider backed by collector for assertions, plus a
tee provider that mirrors each record into the test report - teeProvider when
supplied, otherwise the built-in LogAssertions.TUnit.TestOutputLoggerProvider. Supplying your own provider
lets you plug in correlation that survives background threads (for example TUnit's
CorrelatedTUnitLoggerProvider) without this package taking a dependency on it. Sets the
builder's minimum level and registers the capture floor exactly as AddFakeLogging(ILoggingBuilder, FakeLogCollector, LogLevel) does.
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Both providers are registered into the ILoggingBuilder and become part of the built logger pipeline; their lifetime is the caller's, not this method's.")]
public static ILoggingBuilder AddTeedFakeLogging(this ILoggingBuilder builder, FakeLogCollector collector, LogLevel minimumLevel = LogLevel.Trace, ILoggerProvider? teeProvider = null)
Parameters
builderILoggingBuilderThe logging builder to add the providers to.
collectorFakeLogCollectorThe collector that receives records for assertions.
minimumLevelLogLevelThe minimum level to capture and tee, also registered as the capture floor. Default is Trace (everything).
teeProviderILoggerProviderThe provider that mirrors records into the test report, or null to use the built-in LogAssertions.TUnit.TestOutputLoggerProvider.
Returns
- ILoggingBuilder
The same
builderfor chaining.
Remarks
A supplied teeProvider is registered into the builder and becomes part of the
built logger pipeline. The logging factory does not dispose externally-supplied provider instances,
so dispose it yourself if it holds resources (the usual display/correlation providers do not). The
built-in tee writes to TestContext.Current.Output, so a record logged on a background thread
(where the AsyncLocal context does not flow) is captured but not teed - pass a correlation-aware
provider when that matters.
Exceptions
- ArgumentNullException
builderorcollectoris null.