Skip to content

Logging Generation v1

Kieron Lanning edited this page Mar 21, 2025 · 3 revisions

Logging Gen v1 - Basics


Note:

This is the previous style of log generation, where certain limitations such as a maximum of 6 parameters, single exception parameters, and no support for expanding either properties or enumerations exist.

To use the newer Logging Generation v2, reference (directly or indirectly) the Microsoft.Extensions.Telemetry.Abstractions package.

Also note that ExpandEnumerableAttribute has no affect if used in this type of generation.


The types are hosted within the Purview.Telemetry.Logging namespace.

To signal an interface for logging target generation, decorate the interface with the LoggerAttribute. The signal for a method is the LogAttribute, where you can control various aspects of it's generation.

We also support the generation of scoped loggers.

All log generation is achieved using the LoggerMessage class, as part of the high-performance logging libraries.

Warning

The maximum number of parameters allowed by the LoggerMessage class is 6, plus one optional Exception. You must also reference the Microsoft.Extensions.Logging package on NuGet.

Log Generation

To generate a log entry, the method should be decorated with the LogAttribute and return either void, in the case of a non-scoped log entry. Or IDisposable/ IDisposable? for scoped log entries.

The parameters, if any, are used by the LogAttribute.MessageTemplate and form part of the structured log generation.

There are several options such as the level, name, event Id and message template that can be adjusted via the LogAttribute.

Semantic Level-Based LogAttribute Types

If you prefer, you can use semantic type attributes to specify the level instead of the LogAttribute.Level property.

  • TraceAttribute
  • DebugAttribute
  • InfoAttribute
  • WarningAttribute
  • ErrorAttribute
  • CriticalAttribute

These support all the same properties as LogAttribute, except for the Level property.

Inferring

When not using multi-targeting you can avoid adding the LogAttribute explicitly.

You can set the default log level using either the LoggerAttribute.DefaultLevel on the interface, or the LoggerGenerationAttribute.DefaultLevel on the assembly.

If no level is specified on the method, and an Exception is defined the level is changed to an Error automatically. This also raises the TSG2002 diagnostic, that can safely be ignored if you are comfortable with this inferred behaviour.

Types

LogAttribute

Used to define the log entry details on a method.

Name Type Description
Level Microsoft.Extensions.Logging.LogLevel Determines the level used when defining the log entry. Also available on construction. Defaults to Information, unless an Exception is detected in the parameters, and then Error is used.
MessageTemplate string? Defines the template used to populate the log entry method. If one is not specified, it will automatically be generated based on the prefixes and the available parameters. Also available on construction. Defaults to null.
EventId int? Used when generating the EventId. Also available on construction. Defaults to null. One will be generated if one is not supplied.
Name string? The name of the log entry, if one is not defined then the name of the method is used. Also available on construction. Defaults to null.

LoggerAttribute

Used to enrol an interface in the source generation process.

Name Type Description
DefaultLevel Microsoft.Extensions.Logging.LogLevel Determines the default level to use when one is not provided. Also available on construction. Defaults to Information.
CustomPrefix string? Used when generating the log entry name's prefix. If this is set, the PrefixType is automatically set to Custom. Also available on construction. Defaults to null.
PrefixType Purview.Telemetry.Logging.LogPrefixType Determines the type of prefix to use when generating the log entry name. Defaults to Default.
DisableMSLoggingTelemetryGeneration bool Determines if the Generation v2 style of log generation is enabled or disabled. This defaults to true.

LoggerGenerationAttribute

Used to control defaults at the assembly level.

Name Type Description
DefaultLevel Microsoft.Extensions.Logging.LogLevel Determines the default level to use when one is not provided. Also available on construction. Defaults to Information.
DisableMSLoggingTelemetryGeneration bool Determines if the Generation v2 style of log generation is enabled or disabled across the assembly. This defaults to true.

LogPrefixType

When generating a log entry name, provides options to customise the prefix.

Value Description
Default Does not generate any suffix.
Interface Uses the name of the interface.
Class The name of the class used for generation. This can be specified using the TelemetryGenerationAttribute.ClassName property, or through auto generation.
Custom Used when the LoggerAttribute.CustomPrefix is set.
TrimmedClassName The name of the interface without the "I" prefix or "Log", "Logger" or "Telemetry" suffixes.