Microsoft.Extensions.Diagnostics.Testing
9.0.0-preview.8.24460.1
Prefix Reserved
See the version list below for details.
dotnet add package Microsoft.Extensions.Diagnostics.Testing --version 9.0.0-preview.8.24460.1
NuGet\Install-Package Microsoft.Extensions.Diagnostics.Testing -Version 9.0.0-preview.8.24460.1
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" Version="9.0.0-preview.8.24460.1" />
paket add Microsoft.Extensions.Diagnostics.Testing --version 9.0.0-preview.8.24460.1
#r "nuget: Microsoft.Extensions.Diagnostics.Testing, 9.0.0-preview.8.24460.1"
// Install Microsoft.Extensions.Diagnostics.Testing as a Cake Addin #addin nuget:?package=Microsoft.Extensions.Diagnostics.Testing&version=9.0.0-preview.8.24460.1&prerelease // Install Microsoft.Extensions.Diagnostics.Testing as a Cake Tool #tool nuget:?package=Microsoft.Extensions.Diagnostics.Testing&version=9.0.0-preview.8.24460.1&prerelease
Microsoft.Extensions.Diagnostics.Testing
Hand-crafted fakes to make telemetry-related testing easier.
Install the package
From the command-line:
dotnet add package Microsoft.Extensions.Diagnostics.Testing
Or directly in the C# project file:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" Version="[CURRENTVERSION]" />
</ItemGroup>
Usage Example
Fake logging
These components enable faking logging services for testing purposes.
When using this package, you can register a fake logging provider by one of the following methods:
public static ILoggingBuilder AddFakeLogging(this ILoggingBuilder builder)
public static ILoggingBuilder AddFakeLogging(this ILoggingBuilder builder, IConfigurationSection section)
public static ILoggingBuilder AddFakeLogging(this ILoggingBuilder builder, Action<FakeLogCollectorOptions> configure)
You can also register fake logging in the service collection:
public static IServiceCollection AddFakeLogging(this IServiceCollection services)
public static IServiceCollection AddFakeLogging(this IServiceCollection services, IConfigurationSection section)
public static IServiceCollection AddFakeLogging(this IServiceCollection services, Action<FakeLogCollectorOptions> configure)
After registering the fake logging services, you can resolve the fake logging provider with this method:
public static FakeLogCollector GetFakeLogCollector(this IServiceProvider services)
You can also create an instance of FakeLogger
using one of its constructors:
public FakeLogger(FakeLogCollector? collector = null, string? category = null)
public FakeLogger(Action<string> outputSink, string? category = null)
You can then use it right away, for example:
var fakeLogger = new FakeLogger<MyComponent>();
// Optionally, you can set the log level
// fakeLogger.ControlLevel(LogLevel.Debug, enabled: true);
var myComponentUnderTest = new MyComponent(fakeLogger);
myComponentUnderTest.DoWork(); // We assume that the component will produce some logs
FakeLogCollector collector = fakeLogger.Collector; // Collector allows you to access the captured logs
IReadOnlyList<FakeLogRecord> logs = collector.GetSnapshot();
// ... assert that the logs are correct
Metric collector
The MetricCollector
allows you to collect metrics in tests. It has a few constructors and you can choose the one that fits your needs:
public MetricCollector(Instrument<T> instrument, TimeProvider? timeProvider = null)
public MetricCollector(ObservableInstrument<T> instrument, TimeProvider? timeProvider = null)
public MetricCollector(object? meterScope, string meterName, string instrumentName, TimeProvider? timeProvider = null)
public MetricCollector(Meter meter, string instrumentName, TimeProvider? timeProvider = null)
When you have an exact instrument, you can use the first two constructors. If you have a meter scope (typically it's IMeterFactory
), you can use the third constructor. If you have a meter, use the last one.
Here is an example of how to use the MetricCollector
:
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Diagnostics.Testing;
using var meter = new Meter("TestMeter");
using var collector = new MetricCollector<int>(meter, "TestInstrument");
var myComponentUnderTest = new MyComponent(meter);
myComponentUnderTest.DoWork(); // We assume that the component will produce some integer metrics
CollectedMeasurement<int>? measurement = collector.LastMeasurement();
// ... assert that the measurement is correct
Please note that the MetricCollector
is generic and you need to specify the type of the metric you want to collect (e.g. int
, double
, etc.).
Feedback & Contributing
We welcome feedback and contributions in our GitHub repo.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
.NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.2
- Microsoft.Bcl.TimeProvider (>= 9.0.0-rc.1.24431.7)
- Microsoft.Extensions.Logging (>= 9.0.0-rc.1.24431.7)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0-rc.1.24431.7)
- Microsoft.Extensions.Telemetry.Abstractions (>= 9.0.0-preview.8.24460.1)
- System.Diagnostics.DiagnosticSource (>= 9.0.0-rc.1.24431.7)
-
net8.0
- Microsoft.Extensions.Logging (>= 9.0.0-rc.1.24431.7)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0-rc.1.24431.7)
- Microsoft.Extensions.Telemetry.Abstractions (>= 9.0.0-preview.8.24460.1)
-
net9.0
- Microsoft.Extensions.Logging (>= 9.0.0-rc.1.24431.7)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0-rc.1.24431.7)
- Microsoft.Extensions.Telemetry.Abstractions (>= 9.0.0-preview.8.24460.1)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Microsoft.Extensions.Diagnostics.Testing:
Package | Downloads |
---|---|
Lombiq.Tests.UI
Lombiq UI Testing Toolbox for Orchard Core: Web UI testing toolbox mostly for Orchard Core applications. Everything you need to do UI testing with Selenium for an Orchard app is here. See the project website for detailed documentation. |
|
fm.Extensions.Logging.Testing.MSTest
Logs and reports log messages that have not been handled during UnitTests. |
|
Microsoft.Extensions.Hosting.Testing
Experimental package. Tools for integration testing of apps built with Microsoft.Extensions.Hosting |
|
Lewee.IntegrationTests
Package Description |
GitHub repositories (11)
Showing the top 5 popular GitHub repositories that depend on Microsoft.Extensions.Diagnostics.Testing:
Repository | Stars |
---|---|
dotnet/orleans
Cloud Native application framework for .NET
|
|
MassTransit/MassTransit
Distributed Application Framework for .NET
|
|
EventStore/EventStore
EventStoreDB, the event-native database. Designed for Event Sourcing, Event-Driven, and Microservices architectures
|
|
dotnet/aspire
Tools, templates, and packages to accelerate building observable, production-ready apps
|
|
Cysharp/MagicOnion
Unified Realtime/API framework for .NET platform and Unity.
|
Version | Downloads | Last updated |
---|---|---|
9.0.0 | 6,816 | 11/12/2024 |
9.0.0-preview.9.24507.7 | 2,584 | 10/8/2024 |
9.0.0-preview.8.24460.1 | 1,952 | 9/10/2024 |
9.0.0-preview.7.24412.10 | 2,302 | 8/14/2024 |
9.0.0-preview.6.24353.1 | 1,456 | 7/10/2024 |
9.0.0-preview.5.24311.7 | 764 | 6/11/2024 |
9.0.0-preview.4.24271.2 | 1,415 | 5/21/2024 |
9.0.0-preview.3.24209.3 | 8,815 | 4/11/2024 |
9.0.0-preview.2.24157.4 | 808 | 3/12/2024 |
9.0.0-preview.1.24108.1 | 1,430 | 2/13/2024 |
8.10.0 | 126,346 | 10/8/2024 |
8.9.1 | 116,509 | 9/6/2024 |
8.9.0 | 13,826 | 9/5/2024 |
8.8.0 | 97,763 | 8/13/2024 |
8.7.0 | 119,725 | 7/10/2024 |
8.6.0 | 81,311 | 6/11/2024 |
8.5.0 | 83,499 | 5/14/2024 |
8.4.0 | 62,225 | 4/9/2024 |
8.3.0 | 81,635 | 3/12/2024 |
8.2.0 | 64,639 | 2/13/2024 |
8.1.0 | 46,359 | 1/9/2024 |
8.0.0 | 47,219 | 11/14/2023 |