Skip to content

Generation

Kieron Lanning edited this page Apr 20, 2025 · 4 revisions

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

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 Description
GenerateDependencyExtension bool Controls if the dependency injection class and extension method is generated. This defaults to true.
ClassName string? Explicitly set the name of the class to be generated, rather than using the interface name. This defaults to null, meaning the interface name is used.
DependencyInjectionClassName string? Similar to ClassName, but explicitly sets the name of the DI class name.
DependencyInjectionClassIsPublic bool Setting this to true will generate the DI extension class as public, the default is false, which will make the class internal.

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.

Note

For the generation to compile, make sure the NuGet package Microsoft.Extensions.DependencyInjection is referenced.

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.

Clone this wiki locally