Skip to content

Logging

Kieron Lanning edited this page Feb 18, 2025 · 8 revisions

Logging Generation Types

There are two distinct generated types for ILogger-based generation.

Generation v2

This is the default generation method when the Microsoft.Extensions.Telemetry.Abstractions package is referenced either directly or in-directly.

The generated log implementations more closely resembles the output from the Microsoft.Gen.Logging generator, but with a few additional features.

These include the ability to generate a MessageTemplate dynamically, and enumerate a capture elements in an array/ IEnumerable.

You can opt-out of this new type of generation by explicitly setting by setting LoggingAttribute.DisableMSLoggingTelemetryGeneration to false on your interface definition, or by setting LoggerGenerationAttribute.DisableMSLoggingTelemetryGeneration to false at the assembly level.

More details can be found in the Generation v2 page.

Generation v1

This is the default generation method when Microsoft.Extensions.Telemetry.Abstractions package is not referenced, or when DisableMSLoggingTelemetryGeneration is set to true true on either LoggingAttribute or LoggerGenerationAttribute.

This type of generation utilises the older-style High-performance logging.

More details can be found in the Generation v1 page.

Disabling Generation

To disable logger-based generation in your project add a build constant to either you .csproj or Directory.Build.props with a value of EXCLUDE_PURVIEW_TELEMETRY_LOGGING, and the logging attributes will be ignored.

<!-- In a .csproj -->
<DefineConstants>EXCLUDE_PURVIEW_TELEMETRY_LOGGING</DefineConstants>

<!-- In a Directory.Build.Props -->
<PropertyGroup>
  <DefineConstants>EXCLUDE_PURVIEW_TELEMETRY_LOGGING</DefineConstants>
</PropertyGroup>

This primarily used for when the Microsoft.Extensions.Logging.ILogger type is unavailable. In this case your project will fail to compile due to the generated attributes using related types such as the LogLevel.

Clone this wiki locally