Generation
Basics
Section titled “Basics”The class name is generated from the parent interface with any initial I removed. I.e. ICacheServiceTelemetry would become CacheServiceTelemetry. Then Core is appended, so the final result would be a class named CacheServiceTelemetryCore.
The resulting class is also partial, internal and sealed. It is also generated within the owning namespace and any nested classes of the source interface.
Controlling the Generation
Section titled “Controlling the Generation”By applying the TelemetryGenerationAttribute from the Purview.Telemetry namespace you can control some aspects of generation.
This attribute is permitted on both the assembly and interface. Although take care when using the ClassName and DependencyInjectionClassName at the assembly level, as they will clash.
| Name | Type | Default | Description |
|---|---|---|---|
| GenerateDependencyExtension | bool |
true |
Controls if the dependency injection class and extension method is generated. |
| ClassName | string? |
null |
Explicitly set the name of the class to be generated, rather than using the interface name. When null, the interface name is used. |
| DependencyInjectionClassName | string? |
null |
Similar to ClassName, but explicitly sets the name of the DI class name. |
| DependencyInjectionClassIsPublic | bool |
false |
Setting this to true will generate the DI extension class as public, otherwise it’s internal. |
| NamingConvention | NamingConvention |
OpenTelemetry |
NEW in v4: Specifies which naming convention to use for generated telemetry names. See Naming Conventions below. |
| GenerateTelemetryNamesClass | bool |
true |
Controls whether a static TelemetryNames class is generated containing all meter and activity source names for the assembly. Useful for registering sources with OpenTelemetry (AddServiceDefaults(TelemetryNames.MeterNames, TelemetryNames.ActivitySourceNames)). |
| TelemetryNamesClassName | string? |
null |
Optionally overrides the name of the generated TelemetryNames class. Defaults to "TelemetryNames" when null or empty. |
Naming Conventions
Section titled “Naming Conventions”v4 introduces configurable naming conventions for generated telemetry. The default is OpenTelemetry which follows OpenTelemetry semantic conventions.
Available Options:
public enum NamingConvention{ Legacy = 0, // v3 behaviour: lowercase, smashed compounds OpenTelemetry = 1 // v4 default: OTel conventions (dot.separated, snake_case)}Usage:
// Assembly-wide setting (affects all interfaces)[assembly: TelemetryGeneration(NamingConvention = NamingConvention.OpenTelemetry)]
// Per-interface setting[TelemetryGeneration(NamingConvention = NamingConvention.Legacy)][ActivitySource("MyApp")]interface ILegacyTelemetry{ // Uses v3-style naming}
[TelemetryGeneration(NamingConvention = NamingConvention.OpenTelemetry)][Meter("MyApp")]interface IModernMetrics{ // Uses OpenTelemetry-style naming}Impact on Generated Names:
| Element | Legacy (v3) | OpenTelemetry (v4) |
|---|---|---|
| ActivitySource names | "myapp" |
"MyApp" |
| Tag/Baggage keys | "entityid" |
"entity_id" |
| Metric instrument names | "recordcount" |
"myapp.products.record.count" |
| Metric tag keys | "requestcount" |
"request_count" |
See Breaking Changes - OpenTelemetry-Aligned Naming for detailed migration information.
Dependency Injection
Section titled “Dependency Injection”By default each interface source will have a generated dependency injection extension method, extending the IServiceCollection.
The generated method takes on the Add{InterfaceName} format, note that the interface name has any starting I removed. For example, given the ICacheServiceTelemetry interface, this would generate an extension method called AddCacheServiceTelemetry within the Microsoft.Extensions.DependencyInjection namespace.
When called, the source interface is added to the services collection as a singleton.
The generation can be disabled by setting TelemetryGenerationAttribute.GenerateDependencyExtension to false at the assembly level or on a per-interface basis.
Excluding Methods
Section titled “Excluding Methods”You can exclude any method on an interface by applying the ExcludeAttribute, in the Purview.Telemetry namespace.
As the generated telemetry class is partial, you can implement any excluded methods you require in isolation.