Extends Verify to allow verification of OpenTelemetry types including System.Diagnostics.Activity and LogRecord
See Milestones for release notes.
Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.
[ModuleInitializer]publicstaticvoidInitialize()=>VerifyOpenTelemetry.Initialize();VerifyOpenTelemetry allows, when a method is being tested, for any Activity created as part of that method call to be recorded and verified.
Call Recording.Start() to begin listening. All activities from any ActivitySource will be captured by default.
[Fact]publicTaskUsage(){Recording.Start();usingvarsource=newActivitySource("TestSource");using(varactivity=source.StartActivity("MyOperation")){activity!.SetTag("key1","value1");activity.SetTag("key2",42);}returnVerify("result");}Results in:
{
target: result,
activity: {
MyOperation: {
Tags: {
key1: value1,
key2: 42
}
}
}
}OpenTelemetry LogRecord instances can be captured using InMemoryExporter and verified directly.
[Fact]publicTaskLogRecordVerification(){varlogRecords=newList<LogRecord>();usingvarloggerFactory=LoggerFactory.Create(builder =>{builder.AddOpenTelemetry(options =>{options.AddInMemoryExporter(logRecords);});});varlogger=loggerFactory.CreateLogger("TestCategory");logger.LogInformation("Hello {Name}","World");returnVerify(logRecords);}Results in:
[
{
CategoryName: TestCategory,
LogLevel: Information,
Body: Hello {Name},
Attributes: {
{OriginalFormat}: Hello {Name},
Name: World
}
}
]Activities are serialized with the following conventions:
OperationNameis used as the JSON property keyDisplayNameonly included if different fromOperationNameKindonly included if notInternalStatusandStatusDescriptiononly included if notUnsetTags,Events,Links, andBaggageincluded when present- Non-deterministic values (
Id,TraceId,SpanId,ParentSpanId,Duration,StartTimeUtc,Source) are omitted
LogRecords are serialized with the following conventions:
Timestamp,TraceId,SpanIdare omitted (non-deterministic)CategoryName,LogLevel,Body,FormattedMessageincluded when presentEventIdonly included if non-defaultExceptionincluded when presentAttributesincluded when present
Diagnostic from The Noun Project.
