Skip to content
Purview Dev logo

Logging Generation v1

Stable Telemetry SourceGenerator Reviewed 2026-09-16 purview-dev/telemetry-sourcegenerator Star on GitHub 36 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

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.

PropertyTypeDescription
LevelLogLevelThe level used when defining the log entry. Available on construction. Defaults to Information, unless an Exception is detected in the parameters, then Error.
MessageTemplatestring?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.
EventIdint?Used when generating the EventId. Available on construction. Default null (one is generated if not supplied).
Namestring?The name of the log entry. If not defined, the method name is used. Available on construction. Default null.
GenerationModeLoggerGenerationModePer-method generation-mode override. Auto (default) inherits from the interface/assembly level.
PropertyTypeDescription
DefaultLevelLogLevelThe default level when one is not provided. Available on construction. Default Information.
CustomPrefixstring?Used when generating the log entry name’s prefix. When set, PrefixType is automatically Custom. Available on construction. Default null.
PrefixTypeLogPrefixTypeThe prefix type used when generating the log entry name. Default Default.
GenerationModeLoggerGenerationModeControls the generation mode for all log methods on the interface. Auto (default) selects the best mode per method.
PropertyTypeDescription
DefaultLevelLogLevelThe default level when one is not provided. Available on construction. Default Information.
GenerationModeLoggerGenerationModeControls the generation mode for all log methods in the assembly. Auto (default) selects the best mode per method.
DefaultPrefixTypeLogPrefixTypeThe default prefix type for all log entries in the assembly. Default Default.
ValueDescription
DefaultNo prefix.
InterfaceUses the name of the interface.
ClassThe name of the class used for generation (TelemetryGenerationAttribute.ClassName or auto-generated).
CustomUsed when LoggerAttribute.CustomPrefix is set.
TrimmedClassNameThe interface name without the I prefix or Log, Logger, or Telemetry suffixes.