Repository navigation
·
8 commits
to main
since this release
Immutable
release. Only release title and notes can be modified.
[0.17.0] - 2026-11-24: log definitions generated from [LoggerMessage]
BREAKING
LogDefinition.Propertieswas removed: a definition is the identity of a log call, and the argument values of a capture moved toLogCall.Properties. Migration: capture withLogCall.Capture(...)and readcall.Properties; for the placeholder names alone, readLogDefinition.PlaceholderNames.LogDefinition.Exceptionwas removed, for the same reason. Migration:LogCall.Capture(...), thencall.Exception.LogDefinition.Levelchanged fromLogLeveltoLogLevel?: a generated definition has no level when its method takes the level as a parameter. A captured definition still holds the level of the capture call. Migration:definition.Level ?? ..., ordefinition.Level!.Valuewhere the definition is captured.LogDefinition.Templateisnullfor an empty template: an empty template is no template.LogDefinition.Captureof a[LoggerMessage]method without a message used to return""(Microsoft.Extensions.Logging.Abstractions writes an empty template; Microsoft.Extensions.Telemetry writes none), andLogContractRenderer.RenderandLogDefinition.ToString()wrote""for it; it now returnsnull, and both write<no template>.Matchingtreats an empty template and none alike. Migration: regenerate a contract baseline that holds such a method, and review the diff.LogFilter.MatchingCalltakes aLogCallinstead of aLogDefinition. A generated definition has no argument values, so passing one to the exact-call filter is a build error now, not a filter that never matches. The chain'sMatchingCall(log => ...)is unchanged. Migration:LogFilter.MatchingCall(LogDefinition.Capture(...))becomesLogFilter.MatchingCall(LogCall.Capture(...)).
Added
[assembly: GenerateLogDefinitions(typeof(T))]: a source generator, shipped in the LogAssertions package, writes aLogDefinitionfor each[LoggerMessage]method of the assembly that containsT, at compile time and without reflection. For each type it adds an internal class<Type>LogDefinitionsin the type's namespace with one field per method, so a test writesHasLogged().Matching(OrderServiceLogDefinitions.LogOrderShipped)without a capture lambda or throwaway arguments. A generated definition carries the identity the method's records carry, whichever of Microsoft's two logging generators compiled the product (Microsoft.Extensions.Logging.Abstractions or Microsoft.Extensions.Telemetry): the attribute's event ID, or, when it sets none, the ID the product's logging generator derives, as each version derives it (a hash of the event name; -1 before Microsoft.Extensions.Logging.Abstractions 8.0; 0 before Microsoft.Extensions.Telemetry 8.2; a hash of the C# text for the name in Telemetry 8.2 to 9.7); the event name as the record carries it (a backslash escape in it takes effect, with either generator), or the method name when it is empty or whitespace; the template as the record carries it (with Microsoft.Extensions.Logging.Abstractions before 11.0 a backslash escape in the message takes effect); and the placeholder names in the record's order, including the parameters the template does not use (with Microsoft.Extensions.Telemetry, not a data-classified one). With Microsoft.Extensions.Telemetry, a[TagName],[TagProvider]or[LogProperties]parameter keeps its own name inPlaceholderNames(the first two are not compiled into the product, and the generator does not expand the properties of the third), and so, with Microsoft.Extensions.Logging.Abstractions 8.0 and later, does a parameter of a referenced assembly written with an@its name does not need (a compiled assembly does not record it); matching is not affected. A project that references only LogAssertions.TUnit gets the generator too. The generator sees public and protected methods, and internal ones when the product names the test project inInternalsVisibleTo; a private method is not in the reference assembly a test project compiles against, and the generator applies the same rule to a project reference in an IDE, so the editor and the build agree.GenerateLogDefinitionsAttribute(withTypeInAssembly): the assembly attribute that opts a product assembly in, named by any type in it; apply it once per product assembly.- Generator warnings
LOGA001toLOGA004, reported at theGenerateLogDefinitionsattribute: an opted-in assembly has no[LoggerMessage]method the test project can see; two methods of a type share a name, so neither gets a definition; two types would generate the same class, so neither does; a generated name is taken (a type the test project sees, or a namespace, already has the class's full name, a type the test project sees has the full name of one of its namespaces, or a method is named like the class), so it is left out. LogDefinition.Create(EventId, LogLevel?, string?): builds a definition from its parts, reading the placeholder names from the template, for a record whose logging code a test cannot call.LogDefinition.Create(EventId, LogLevel?, string?, IEnumerable<string>): the same with the placeholder names as given. The generated definitions use it.LogDefinition.PlaceholderNames: the names of the placeholder values a record of the definition carries.LogCall: one captured log call,LogCall.Capture(log => ...), with itsDefinition,Properties, andException, forLogFilter.MatchingCall.
Changed
LogContractRenderer.Renderwrites?as the level of a definition without one (a generated definition whose method takes the level as a parameter). Lines of captured definitions are unchanged.- README: a section on the generated definitions, and the recommendation to give each
[LoggerMessage]method an explicitEventId, so a renamed method keeps its event ID.