Purview.Telemetry.SourceGenerator 4.0.0-alpha.3

This is a prerelease version of Purview.Telemetry.SourceGenerator.
dotnet add package Purview.Telemetry.SourceGenerator --version 4.0.0-alpha.3
                    
NuGet\Install-Package Purview.Telemetry.SourceGenerator -Version 4.0.0-alpha.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="4.0.0-alpha.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Purview.Telemetry.SourceGenerator" Version="4.0.0-alpha.3" />
                    
Directory.Packages.props
<PackageReference Include="Purview.Telemetry.SourceGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Purview.Telemetry.SourceGenerator --version 4.0.0-alpha.3
                    
#r "nuget: Purview.Telemetry.SourceGenerator, 4.0.0-alpha.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Purview.Telemetry.SourceGenerator@4.0.0-alpha.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Purview.Telemetry.SourceGenerator&version=4.0.0-alpha.3&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Purview.Telemetry.SourceGenerator&version=4.0.0-alpha.3&prerelease
                    
Install as a Cake Tool

Purview Telemetry Source Generator

Generates ActivitySource, ILogger, and Metrics based telemetry from methods you define on an interface.

CI

Features

  • Zero boilerplate - define methods on an interface, get full telemetry implementation generated
  • Multi-target generation - generate Activities, Logging, and Metrics from a single interface
  • Testable - easy mocking/substitution for unit testing
  • DI-ready - automatic dependency injection registration helpers

Supported Frameworks

  • .NET Framework 4.8
  • .NET 8 or higher

Installation

Add to your Directory.Build.props or .csproj file:

<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="4.0.0-alpha.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

Quick Start

Define an interface with telemetry methods and the generator creates the implementation:

using Purview.Telemetry;

// Multi-target interface: generates Activities, Logging, AND Metrics from combined methods
[ActivitySource]
[Logger]
[Meter]
interface IEntityStoreTelemetry
{
    // MULTI-TARGET: Creates Activity + Logs Info + Increments Counter - all from one method!
    [Activity]
    [Info]
    [AutoCounter]
    Activity? GettingEntityFromStore(int entityId, [Baggage]string serviceUrl);

    // MULTI-TARGET: Adds ActivityEvent + Logs the duration
    [Event]
    [Log]
    void GetDuration(Activity? activity, int durationInMS);

    // Single-target examples (when you only need one telemetry type):
    
    // Activity-only: Adds Baggage to the Activity
    [Context]
    void RetrievedEntity(Activity? activity, float totalValue, int lastUpdatedByUserId);

    // Log-only: Structured log message
    [Warning]
    void EntityNotFound(int entityId);

    // Metric-only: Histogram for tracking values
    [Histogram]
    void RecordEntitySize(int sizeInBytes);
}

Register with dependency injection:

// Generated extension method
services.AddEntityStoreTelemetry();

Then inject and use - a single method call emits an Activity, Log, and Metric simultaneously:

public class EntityService(IEntityStoreTelemetry telemetry)
{
    public async Task<Entity> GetEntityAsync(int id, string serviceUrl, CancellationToken cancellationToken)
    {
        // Single call creates Activity AND logs AND increments counter
        using var activity = telemetry.GettingEntityFromStore(id, serviceUrl);
        
        var entity = await _repository.GetAsync(id, cancellationToken);
                        
        // Adds event to activity AND logs duration
        telemetry.GetDuration(activity, stopwatch.ElapsedMilliseconds);

        if (entity == null)
        {
            // Logs warning if entity not found
            telemetry.EntityNotFound(id);
            return null;
        })

        // Activity context addition
        telemetry.RetrievedEntity(activity, entity.TotalValue, entity.LastUpdatedByUserId);
        
        // Histogram only records size
        telemetry.RecordEntitySize(entity.SizeInBytes);

        return entity;
    }
}

Telemetry Types

Attribute Generation Type Description
[ActivitySource] Class-level Marks interface for Activity generation
[Activity] Method Creates and starts a new Activity
[Event] Method Adds an ActivityEvent to an Activity
[Context] Method Adds Baggage to an Activity
[Logger] Class-level Marks interface for ILogger generation
[Log] Method Generates structured log message
[Debug], [Info], [Warning], [Error], [Critical] Method Log with specific level
[Meter] Class-level Marks interface for Metrics generation
[Counter], [AutoCounter] Method Counter instrument
[Histogram] Method Histogram instrument
[ObservableCounter], [ObservableGauge], [ObservableUpDownCounter] Method Observable instruments

For single-target interfaces (only Activities, only Logging, or only Metrics), the generator automatically infers the necessary attributes. See the wiki for details.

Documentation

Sample Project

The .NET Aspire Sample demonstrates Activities, Logs, and Metrics generation working together with the Aspire Dashboard.

The sample project has EmitCompilerGeneratedFiles enabled so you can inspect the generated output.

v4 Breaking Changes

Namespace Consolidation

v4 consolidates all attributes into a single namespace. Update your using statements:

Before (v3):

using Purview.Telemetry.Activities;
using Purview.Telemetry.Logging;
using Purview.Telemetry.Metrics;

After (v4):

using Purview.Telemetry;

All attributes ([ActivitySource], [Logger], [Meter], [Activity], [Event], [Log], [Counter], etc.) are now in the unified Purview.Telemetry namespace.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.0-alpha.3 50 1/10/2026
3.2.4 496 7/27/2025
3.2.3 171 7/11/2025
3.2.0 404 4/24/2025
3.1.0 243 4/20/2025
3.0.0 219 2/19/2025
3.0.0-prerelease.7 109 2/18/2025
3.0.0-prerelease.6 110 2/18/2025
3.0.0-prerelease.5 136 2/17/2025
3.0.0-prerelease.4 113 2/17/2025
3.0.0-prerelease.3 139 2/17/2025
3.0.0-prerelease.2 127 2/16/2025
3.0.0-prerelease.1 141 2/16/2025
2.0.1 180 2/2/2025
2.0.0 196 2/1/2025
1.1.0 180 12/8/2024
1.0.12 216 6/9/2024
1.0.11 243 6/3/2024
1.0.10 173 6/3/2024
1.0.9 193 5/30/2024
1.0.8 195 5/27/2024
1.0.7 184 5/20/2024
1.0.6 213 5/20/2024
1.0.5 209 5/6/2024
1.0.4 186 5/1/2024
1.0.3 406 4/30/2024
1.0.2 198 4/25/2024
1.0.1 187 4/25/2024
1.0.0 227 4/22/2024
0.0.14-prerelease 205 4/8/2024
0.0.13-prerelease 200 4/7/2024
0.0.12-prerelease 167 4/6/2024
0.0.11-prerelease 181 4/6/2024
0.0.10-prerelease 193 4/6/2024