Skip to content
Purview Dev logo

Documentation section: Reference

Diagnostics

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 package ships a single Roslyn analyzer, TelemetryDiagnosticAnalyzer, which validates telemetry interfaces and raises diagnostics with the TSG prefix. All diagnostics are grouped by category.

  • General — TSG1xxx
  • Logging — TSG2xxx
  • Activities — TSG3xxx
  • Metrics — TSG4xxx
ID Severity Description
TSG1000 Error Fatal execution error occurred (Failed to execute the generation stage: {0}). Raised by the generator itself when an unexpected exception escapes the pipeline.
TSG1001 Error Inferring generation targets is not supported when using multi-target generation. A method on a multi-target interface has no explicit generation attribute.
TSG1002 Error Multiple attributes from the same target family are not supported. Only one Activity, Logging, or Metrics attribute is allowed per method.
TSG1003 Error Duplicate method names are not supported. Two or more methods on the interface share the same name.
TSG1004 Error Generic interface cannot be generated. A generic interface is generated as a generic class with the same type parameters and constraints; the shapes that cannot be are a nested generic interface (the enclosing type’s type arguments cannot be repeated on the generated class) and a type parameter carrying the allows ref struct anti-constraint (the generated class cannot repeat it).
TSG1005 Error Generic method cannot be generated. Generic methods and their constraints are mirrored; what cannot be is a type parameter carrying the allows ref struct anti-constraint, or a metric method whose measurement is one of its own type parameters (the instrument is a field, and a field cannot name a method’s type parameter — declare that type parameter on the interface instead). The method is emitted as a throwing stub so the interface stays implemented.
TSG1006 Warning ExcludeTargets references a target not present on this method.
TSG1007 Warning ExcludeTargets results in an empty or invalid parameter set for a target.
TSG1008 Warning Activity parameter has no Activity target. A parameter of type Activity is present on a method with no [Activity]/[Event]/[Context] attribute.
TSG1010 Error Method target not registered on interface. A method carries an attribute for a target family that is not registered on the interface.
TSG1011 Error Unsupported target framework. The compilation targets neither .NET 10+ nor .NET Framework 4.8+ (for example netstandard2.0, net8.0 or net9.0), so no telemetry is generated for it. There is no opt-out. Target net10.0 or later, or net48 or later; a multi-targeted library references the package only from its supported targets (a Condition on the PackageReference), with the telemetry interfaces under the same condition. See Platform Support.
ID Severity Description
TSG2000 Error Too many exception parameters. A non-scoped log method has more than one Exception-typed parameter.
TSG2001 Error More than 6 parameters. A log method in v1 generation mode has more than 6 non-exception parameters.
TSG2002 Info Inferring error log level. In v1 generation, a single Exception parameter is present with no explicit level, so Error is inferred.
TSG2003 Warning Could not find a reference to Microsoft.Extensions.Logging.ILogger, skipping log generation.
TSG2004 Error Cannot mix ordinal and named property placeholders in a message template.
TSG2005 Error Ordinal values exceed parameter count. The maximum ordinal placeholder value exceeds the number of provided parameters.
TSG2006 Error Using [LogProperties] and [ExpandEnumerable] on the same parameter is not supported.
TSG2007 Warning A scoped log shouldn’t have a LogLevel; it will be ignored.
TSG2008 Warning Unbounded enumeration possible. [ExpandEnumerable] has a MaximumValueCount greater than the recommended default of 5.
TSG2021 Error Log method must return void or IDisposable. (Returning Activity is allowed when the method is also an Activity method.)
TSG2022 Error params parameters are not supported. The generated method re-declares the parameter without the modifier, so the params call form would stop compiling. Take an array and mark it [ExpandEnumerable] instead. Parity with the .NET 11 [LoggerMessage] generator’s SYSLIB1028.
TSG2023 Error ref, out, in, scoped, and ref struct parameters (such as Span<T>) are not supported: the generated state struct cannot capture them. Parity with the .NET 11 [LoggerMessage] generator’s SYSLIB1029.
TSG2024 Error The [TagProvider] type could not be resolved. The parameter is logged without the provider’s extra tags.
TSG2025 Error The [TagProvider] method could not be found on the provider type. The parameter is logged without the provider’s extra tags.
TSG2026 Error The [TagProvider] method has an invalid signature. It must be a visible static method taking one parameter the annotated parameter can be passed to, and returning IEnumerable<KeyValuePair<string, object?>> (or the string-valued variant, or a type implementing one). The parameter is logged without the provider’s extra tags.
TSG2027 Warning [TagProvider] is on a method that generates no log entry, so the provider would never run. Move it to a logging method, or remove it.
ID Severity Description
TSG3000 Warning Baggage parameter types only accept strings (ToString() will be called).
TSG3001 Warning No activity source specified. Generation defaults to purview when no name is available anywhere.
TSG3002 Error Invalid return type. An Activity/Event method returns something other than void or System.Diagnostics.Activity.
TSG3003 Error Duplicate reserved parameters defined. More than one parameter maps to the same reserved destination.
TSG3004 Error Activity parameter is not valid. An Activity-destination parameter appears on an Activity method (only valid on [Event] methods).
TSG3005 Error Timestamp parameter is not valid. A timestamp parameter appears on a method that is not an [Event] method.
TSG3006 Error Start time parameter is not valid on Create activity or Event method.
TSG3007 Error Parent context or Parent Id parameter is not valid on event.
TSG3008 Error Activity links parameters are not valid on events or context methods.
TSG3009 Error Activity tags parameter is not valid on context methods.
TSG3010 Error Escaped parameters must be a boolean.
TSG3011 Error Escaped parameters are only valid on Events, not Activity or Context methods.
TSG3012 Info There are no Activity methods defined, assumed use of Activity.Current.
TSG3013 Warning Should return the created Activity. An Activity method does not return the created Activity.
TSG3014 Warning Should accept an Activity to apply the Event/Tags/Baggage to. An Event/Context method has no Activity parameter. Opt-in via ActivitySourceGeneration.GenerateDiagnosticsForMissingActivity (default true).
TSG3015 Info Activity should be the first parameter. Opt-in via GenerateDiagnosticsForMissingActivity.
TSG3016 Error Status description parameter should be a string.
TSG3017 Error Status Description parameters are only valid on Events, not Activity or Context methods.
TSG3021 Info Exception event does not use OpenTelemetry standard name. An [Event] method records an exception under the OpenTelemetry exception rules but the event name is not the standard "exception" (suggest [Event(Name = "exception")]). Not raised when UseRecordExceptionRules is disabled, for [Baggage] exceptions, or when the exception parameter is excluded from the Activities target.
TSG3022 Warning Activity return type should be nullable. An Activity method returns non-nullable Activity; use Activity? because the Activity can be null when no listeners are active.
TSG3023 Warning ActivitySourceFactory is not available. UseActivitySourceFactory is set but System.Diagnostics.ActivitySourceFactory (.NET 11, or System.Diagnostics.DiagnosticSource 11+) cannot be resolved; a static ActivitySource is generated instead.
TSG3024 Warning ActivitySourceOptions require UseActivitySourceFactory. Version or TelemetrySchemaUrl is set but the assembly kept the static ActivitySource, which has nowhere to pass them.
ID Severity Description
TSG4000 Error No instrument defined. A method on a Metrics interface has no instrument attribute and is not excluded.
TSG4001 Error Must return void or bool. A metrics-owned method returns something other than void or bool.
TSG4002 Error Auto increment counter and measurement defined. An auto-increment instrument also has a measurement parameter.
TSG4003 Error Multiple measurement values defined.
TSG4004 Error No measurement value defined. A non-auto-increment instrument method has no measurement parameter.
TSG4005 Error Observable instrument requires Func<T>.
TSG4006 Error Invalid measurement type. Not one of byte, short, int, long, double, float, decimal, Measurement<T>, or IEnumerable<Measurement<T>>.
TSG4007 Error Observable metrics cannot return bool.
TSG4008 Error AutoCounter must return void.
TSG4009 Warning Instrument name matches the instrument type name. Use a name that describes what is measured.
TSG4010 Warning Gauge is not available. [Gauge] needs System.Diagnostics.Metrics.Gauge<T> (.NET 9, or System.Diagnostics.DiagnosticSource 9+); the instrument is not generated and the method throws NotSupportedException.
TSG4011 Warning HistogramBucketBoundaries is not available. It needs InstrumentAdvice<T> (.NET 9, or System.Diagnostics.DiagnosticSource 9+); the histogram is generated without bucket boundaries.
TSG4012 Warning HistogramBucketBoundaries is not a list of numbers. It must be a comma-separated list of ascending, positive numbers, for example "0.005, 0.01, 0.1, 1".

TSG1001 — inference disabled on multi-target interfaces

Section titled “TSG1001 — inference disabled on multi-target interfaces”

A method on a multi-target interface ([ActivitySource] + [Logger] + [Meter]) has no explicit attribute. Add the attributes for each target the method should emit, or mark it [Exclude].

[ActivitySource("MyApp")]
[Logger]
[Meter]
interface IMyTelemetry
{
[Info] // ✅ explicit target
void ProcessItem(int id);
}

Two or more methods share the same name, which is used to generate members on the implementation class. Rename the methods.

TSG1004 — generic interface cannot be generated

Section titled “TSG1004 — generic interface cannot be generated”

A generic interface is generated as a generic class with the same type parameters and constraints:

[Logger]
public interface IMyTelemetry<T> where T : class
{
void Log(T value); // ✅ generated as IMyTelemetryCore<T>
}

Two shapes still cannot be generated, because the generated class has no way to express them:

  • a nested generic interface — the enclosing type’s type arguments cannot be repeated on the generated class;
  • a type parameter carrying the allows ref struct anti-constraint — it cannot be repeated on the generated class.

TSG1005 — generic method cannot be generated

Section titled “TSG1005 — generic method cannot be generated”

Generic methods are generated with the interface method’s type parameters and constraints mirrored onto the implementation, and constraints are mirrored for struct, class, class?, notnull, unmanaged, new() and type-parameter or type constraints:

[Logger]
public interface IMyTelemetry
{
void Log<T>(T value) where T : notnull; // ✅ generated as void Log<T>(T value) where T : notnull
}

The method is emitted as a throwing stub, so the interface stays implemented, when:

  • a type parameter carries the allows ref struct anti-constraint; or
  • a metric method uses one of its own type parameters as the instrument measurement. An instrument is a field, and a field cannot name a method’s type parameter — declare that type parameter on the interface instead, which makes the generated class generic and lets the instrument field use it.

TSG2024 / TSG2025 / TSG2026 — [TagProvider] cannot be used

Section titled “TSG2024 / TSG2025 / TSG2026 — [TagProvider] cannot be used”

[TagProvider(typeof(T), "Method")] names a static method that contributes extra tags; see Logging. TSG2024 reports a provider type that cannot be resolved, TSG2025 a method that does not exist on it, and TSG2026 a method whose signature the generated foreach cannot read. In every case the parameter is still logged — only the extra tags are missing.

TSG2027 — [TagProvider] on a method that never logs

Section titled “TSG2027 — [TagProvider] on a method that never logs”

The attribute is on a parameter of a method that generates no log entry (for example an [Activity]-only method on a multi-target interface). Move it to the logging method, or remove it.

[ExpandEnumerable] is configured with a MaximumValueCount greater than 5. Reduce it to the recommended default (5) unless you have tested the performance impact.

An Activity method does not return the created Activity, or an Event/Context method has no Activity parameter. Return the Activity/Activity? and pass it to Event/Context methods. These best-practice diagnostics are controlled by ActivitySourceGeneration.GenerateDiagnosticsForMissingActivity.

Rename the event with [Event(Name = "exception")] so the OpenTelemetry exception tags (exception.type, exception.message, exception.stacktrace, exception.escaped) are attached to that event instead of a separate event named exception.

[Event(Name = "exception")] // ✅ the event carries the exception tags
void FailedToRetrieve(Activity? activity, Exception exception);

A Name set on a logging attribute does not name the event — it renames the log entry:

[Event] // ❌ event name is the method name
[Error(Name = "exception")] // the log entry is named 'exception'
void FailedToRetrieve(Activity? activity, Exception exception);

The diagnostic is not raised when the exception is not recorded using the OpenTelemetry exception rules: [Event(UseRecordExceptionRules = false)], a [Baggage] exception parameter, or an exception parameter excluded from the Activities target ([ExcludeTargets(Targets.Activities)]).

Return Activity? so callers can handle the null case when no listeners are active.

TSG3023 — ActivitySourceFactory is not available

Section titled “TSG3023 — ActivitySourceFactory is not available”

[assembly: ActivitySourceGeneration(..., UseActivitySourceFactory = true)] asks for sources created by System.Diagnostics.ActivitySourceFactory, but the compilation cannot resolve that type, so a static ActivitySource is generated instead. Target net11.0, or reference System.Diagnostics.DiagnosticSource 11 or later; or remove the option. See Creating sources with ActivitySourceFactory.

TSG3024 — ActivitySourceOptions require UseActivitySourceFactory

Section titled “TSG3024 — ActivitySourceOptions require UseActivitySourceFactory”

Version or TelemetrySchemaUrl on [assembly: ActivitySourceGeneration(...)] is only consumed by System.Diagnostics.ActivitySourceOptions, which a factory-created source passes to ActivitySourceFactory.Create. A static ActivitySource has nowhere to put them, so they are ignored. Set UseActivitySourceFactory = true, or remove the option. See Creating sources with ActivitySourceFactory.

TSG4002 — auto-increment counter with a measurement

Section titled “TSG4002 — auto-increment counter with a measurement”

[AutoCounter] (or [Counter(AutoIncrement = true)]) must not declare a measurement parameter — the measurement is fixed to 1.