Purview.Telemetry.SourceGenerator 3.0.0

dotnet add package Purview.Telemetry.SourceGenerator --version 3.0.0                
NuGet\Install-Package Purview.Telemetry.SourceGenerator -Version 3.0.0                
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="3.0.0">
  <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.
paket add Purview.Telemetry.SourceGenerator --version 3.0.0                
#r "nuget: Purview.Telemetry.SourceGenerator, 3.0.0"                
#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.
// Install Purview.Telemetry.SourceGenerator as a Cake Addin
#addin nuget:?package=Purview.Telemetry.SourceGenerator&version=3.0.0

// Install Purview.Telemetry.SourceGenerator as a Cake Tool
#tool nuget:?package=Purview.Telemetry.SourceGenerator&version=3.0.0                

Purview Telemetry Source Generator

Generates ActivitySource, ILogger, and Metrics based on interface methods.

This approach allows for:

  • Faster iteration cycles - simply create the method on the interface
  • Easy substitution for testing testing - for an example, check the sample project
  • Built-in dependency injection helper generation

Use the latest version available on NuGet, which supports the following frameworks:

  • .NET Framework 4.7.2, or higher
  • .NET 8 or higher

Reference in your Directory.Build.props or .csproj file:

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

For more information see the wiki.

Example Interface

This is called a multi-target interface because it generates more than one output type: Activities, Logging, and Metrics.

When generating a single target, the generator will automatically infer the necessary attributes. More information about multi-targeting can be found in here.

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

/// <summary>
/// Generates an implementation of the methods for each generation type (Activity, Logging, or Metrics)
/// and an extension method to enable easy registration with the IServiceCollection.
/// </summary>
[ActivitySource]
[Logger]
[Meter]
interface IEntityStoreTelemetry
{
    /// <summary>
    /// Creates and starts an Activity and adds the parameters as Tags and Baggage.
    /// </summary>
    [Activity]
    Activity? GettingEntityFromStore(int entityId, [Baggage]string serviceUrl);

    /// <summary>
    /// Adds an ActivityEvent to the Activity with the parameters as Tags.
    /// </summary>
    [Event]
    void GetDuration(Activity? activity, int durationInMS);

    /// <summary>
    /// Adds the parameters as Baggage to the Activity.
    /// </summary>
    [Context]
    void RetrievedEntity(Activity? activity, float totalValue, int lastUpdatedByUserId);

    /// <summary>
    /// Generates a structured log message using an ILogger - defaults to Informational.
    /// </summary>
    [Log]
    void ProcessingEntity(int entityId, string updateState);

    /// <summary>
    /// Generates a structured log message using an ILogger, specifically defined as Informational.
    /// </summary>
    [Info]
    void ProcessingAnotherEntity(int entityId, string updateState);

    /// <summary>
    /// Adds 1 to a Counter<T> with the entityId as a Tag.
    /// </summary>
    [AutoCounter]
    void RetrievingEntity(int entityId);
}

To see the code generated for the IEntityStoreTelemetry interface, see the Generated Output page in the wiki.

Example Project

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

Check the page in the the wiki for information.

This sample project has EmitCompilerGeneratedFiles set to true, so you can easily see the generated output.

Notes on Logging Generation

There are two types of logging generation based on:

  1. Microsoft Logging Extension Packages – Determined by the NuGet packages referenced in your project.
  2. Attribute-based Configuration – Controlled using attributes in your code.

For more details, see the Logging page in the wiki.

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
3.0.0 44 2/19/2025
3.0.0-prerelease.7 33 2/18/2025
3.0.0-prerelease.6 35 2/18/2025
3.0.0-prerelease.5 37 2/17/2025
3.0.0-prerelease.4 37 2/17/2025
3.0.0-prerelease.3 35 2/17/2025
3.0.0-prerelease.2 34 2/16/2025
3.0.0-prerelease.1 38 2/16/2025
2.0.1 83 2/2/2025
2.0.0 73 2/1/2025
1.1.0 103 12/8/2024
1.0.12 137 6/9/2024
1.0.11 146 6/3/2024
1.0.10 110 6/3/2024
1.0.9 126 5/30/2024
1.0.8 117 5/27/2024
1.0.7 121 5/20/2024
1.0.6 127 5/20/2024
1.0.5 138 5/6/2024
1.0.4 108 5/1/2024
1.0.3 340 4/30/2024
1.0.2 122 4/25/2024
1.0.1 115 4/25/2024
1.0.0 124 4/22/2024
0.0.14-prerelease 112 4/8/2024
0.0.13-prerelease 112 4/7/2024
0.0.12-prerelease 102 4/6/2024
0.0.11-prerelease 108 4/6/2024
0.0.10-prerelease 103 4/6/2024