Skip to content
Purview Dev logo

Documentation section: Telemetry

Logging Generation v1

Stable Telemetry SourceGenerator Reviewed 2026-11-15 purview-dev/telemetry-sourcegenerator Star on GitHub 37 activity c-sharp distributed-tracing dotnet events high-performance-logging instrumentation logging melt metrics nuget observability open-telemetry open-telemetry-csharp otel roslyn source-generator spans telemetry tracing

The original LoggerMessage.Define-based logging mode: what it emits, its limits, and when it is still generated.

Generation v1 is the previous style of log generation, built on LoggerMessage.Define from the high-performance logging libraries.

It has these limitations:

  • A maximum of 6 non-exception parameters.
  • At most one Exception parameter.
  • No support for expanding enumerations ([ExpandEnumerable] has no effect).
  • No support for [LogProperties].

v1 is used automatically in LoggerGenerationMode.Auto for any method that is within the v1 limits. You can force it for all methods on an interface or assembly:

[Logger(GenerationMode = LoggerGenerationMode.V1)]
interface IOrderServiceTelemetry { }

To generate a log entry, decorate the method with [Log] and return either void (non-scoped) or IDisposable/IDisposable? (scoped). Parameters form part of the structured log generation and are referenced by the MessageTemplate.

You can also use the semantic level attributes instead of [Log].Level:

  • [Trace], [Debug], [Info], [Warning], [Error], [Critical]

These support all the same properties as [Log], except Level.

When not using multi-targeting you can omit [Log] entirely — every method on a [Logger] interface becomes a log method. Set the default level with [Logger].DefaultLevel (interface) or [LoggerGeneration].DefaultLevel (assembly).

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

Property Type Description
Level LogLevel The level used when defining the log entry. Available on construction. Defaults to Information, unless an Exception is detected in the parameters, then Error.
MessageTemplate string? The template used to populate the log entry. If not specified, one is generated from the prefixes and available parameters. Available on construction. Default null.
EventId int? Used when generating the EventId. Available on construction. Default null (one is generated if not supplied).
Name string? The name of the log entry. If not defined, the method name is used. Available on construction. Default null.
GenerationMode LoggerGenerationMode Per-method generation-mode override. Auto (default) inherits from the interface/assembly level.
Property Type Description
DefaultLevel LogLevel The default level when one is not provided. Available on construction. Default Information.
CustomPrefix string? Used when generating the log entry name’s prefix. When set, PrefixType is automatically Custom. Available on construction. Default null.
PrefixType LogPrefixType The prefix type used when generating the log entry name. Default Default.
GenerationMode LoggerGenerationMode Controls the generation mode for all log methods on the interface. Auto (default) selects the best mode per method.
Property Type Description
DefaultLevel LogLevel The default level when one is not provided. Available on construction. Default Information.
GenerationMode LoggerGenerationMode Controls the generation mode for all log methods in the assembly. Auto (default) selects the best mode per method.
DefaultPrefixType LogPrefixType The default prefix type for all log entries in the assembly. Default Default.
Value Description
Default No prefix.
Interface Uses the name of the interface.
Class The name of the class used for generation (TelemetryGenerationAttribute.ClassName or auto-generated).
Custom Used when LoggerAttribute.CustomPrefix is set.
TrimmedClassName The interface name without the I prefix or Log, Logger, or Telemetry suffixes.