Skip to content

.NET 5.0+ (included in SDK)

Stable Telemetry SourceGenerator Reviewed 2026-09-01 purview-dev/telemetry-sourcegenerator 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

This guide covers installing and configuring the Purview Telemetry Source Generator in your project.

  • .NET 8.0 or higher (Recommended)
  • .NET Framework 4.8 or higher
  • Visual Studio 2022 (17.0 or higher)
  • Visual Studio Code with C# extension
  • JetBrains Rider 2022.3 or higher
  • Any IDE with Roslyn analyzer support
Terminal window
dotnet add package Purview.Telemetry.SourceGenerator --version 5.0.0-prerelease.1

Method 2: Package Manager Console (Visual Studio)

Section titled “Method 2: Package Manager Console (Visual Studio)”
Terminal window
Install-Package Purview.Telemetry.SourceGenerator -Version 5.0.0-prerelease.1

Add directly to your .csproj file:

<ItemGroup>
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

Method 4: Directory.Build.props (Multi-Project Solutions)

Section titled “Method 4: Directory.Build.props (Multi-Project Solutions)”

For solutions with multiple projects that all need telemetry, create or edit Directory.Build.props at the solution root:

<Project>
<ItemGroup>
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

This automatically adds the package to all projects in the solution.

Create a file ITelemetryTest.cs:

using Purview.Telemetry;
[Logger]
public interface ITelemetryTest
{
[Info]
void TestMessage(string message);
}
Terminal window
dotnet build

If EmitCompilerGeneratedFiles is enabled (see below), check:

obj/Debug/net8.0/generated/Purview.Telemetry.SourceGenerator/

You should see generated files with names like:

  • {Namespace}.TelemetryTestCore.Logging.g.cs
  • {Namespace}.TelemetryTestCoreDIExtension.DependencyInjection.g.cs

Step 4: Use the Generated Extension Method

Section titled “Step 4: Use the Generated Extension Method”

In your Program.cs:

services.AddTelemetryTest(); // This should have IntelliSense support

If IntelliSense recognizes AddTelemetryTest(), installation was successful! ✅

Section titled “Enable Generated File Output (Recommended for Development)”

To inspect generated code during development, add to your .csproj:

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)/generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

Generated files appear in obj/Debug/net8.0/generated/.

To include source generator attributes in your assembly (usually not needed):

<PropertyGroup>
<DefineConstants>PURVIEW_TELEMETRY_ATTRIBUTES</DefineConstants>
</PropertyGroup>

By default, attributes are marked [Conditional("PURVIEW_TELEMETRY_ATTRIBUTES")] and are removed at runtime.

If Microsoft.Extensions.Logging is not available in your project:

<PropertyGroup>
<DefineConstants>EXCLUDE_PURVIEW_TELEMETRY_LOGGING</DefineConstants>
</PropertyGroup>

This excludes logging-related attributes and code generation.

The source generator itself has no runtime dependencies. However, to use the generated code, you need:

Terminal window
# .NET 5.0+ (included in SDK)
# No additional packages needed
Terminal window
dotnet add package Microsoft.Extensions.Logging
dotnet add package Microsoft.Extensions.Telemetry.Abstractions
Terminal window
dotnet add package Microsoft.Extensions.Logging
Terminal window
# .NET 8.0+ (included in SDK)
# No additional packages needed for IMeterFactory

To export telemetry to observability platforms:

Terminal window
# Core packages
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.Console
# Optional: Specific exporters
dotnet add package OpenTelemetry.Exporter.Jaeger
dotnet add package OpenTelemetry.Exporter.Prometheus.AspNetCore
dotnet add package Azure.Monitor.OpenTelemetry.Exporter
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="8.0.0" />
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

See the Sample Application for a complete .NET Aspire example.

If your project targets multiple frameworks:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<!-- Conditional dependencies for older frameworks -->
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" />
</ItemGroup>
</Project>
  1. Install latest Visual Studio 2022 (17.8 or higher recommended)
  2. Ensure “Source Generators” support is enabled (on by default)
  3. After adding the package, rebuild to trigger generation
  4. Generated files appear in Solution Explorer under Dependencies > Analyzers > Purview.Telemetry.SourceGenerator
  1. Install the C# extension (ms-dotnettools.csharp)
  2. Ensure the latest .NET SDK is installed
  3. Open the project folder
  4. Run dotnet build from the terminal
  5. IntelliSense should recognize generated types after build
  1. Install Rider 2022.3 or higher
  2. Rider automatically detects source generators
  3. After adding the package, rebuild the solution
  4. Generated types appear in IntelliSense immediately

Issue: “Type or namespace ‘Purview’ could not be found”

Section titled “Issue: “Type or namespace ‘Purview’ could not be found””

Cause: Package not installed or build not run.

Solution:

Terminal window
dotnet restore
dotnet build

Issue: “No extension method ‘Add{InterfaceName}’ found”

Section titled “Issue: “No extension method ‘Add{InterfaceName}’ found””

Cause: Source generator didn’t run or interface not properly annotated.

Solution:

  1. Ensure the interface has [ActivitySource], [Logger], or [Meter] attribute
  2. Rebuild: dotnet build --no-incremental
  3. Check for build errors in the output

Issue: “Generated files not visible in IDE”

Section titled “Issue: “Generated files not visible in IDE””

Cause: EmitCompilerGeneratedFiles not enabled.

Solution: Add to .csproj:

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

Issue: “Source generator errors during build”

Section titled “Issue: “Source generator errors during build””

Cause: Incompatible package version or corrupted NuGet cache.

Solution:

Terminal window
# Clear NuGet cache
dotnet nuget locals all --clear
# Restore packages
dotnet restore
# Clean and rebuild
dotnet clean
dotnet build

Issue: “Attributes not recognized (red squiggles)”

Section titled “Issue: “Attributes not recognized (red squiggles)””

Cause: IDE hasn’t detected the generator yet.

Solution:

  1. Rebuild the project: dotnet build
  2. Restart the IDE
  3. Check the package is correctly referenced in .csproj

Issue: “Multiple definitions of type ‘ActivitySourceAttribute’”

Section titled “Issue: “Multiple definitions of type ‘ActivitySourceAttribute’””

Cause: Package referenced multiple times or in multiple Directory.Build.props files.

Solution: Remove duplicate references and ensure the package is only included once per project.

If you’re upgrading from v3.x to v4.x:

<!-- Before -->
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1" />
<!-- After -->
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="5.0.0-prerelease.1" />
// Before (v3)
using Purview.Telemetry.Activities;
using Purview.Telemetry.Logging;
using Purview.Telemetry.Metrics;
// After (v4)
using Purview.Telemetry; // Single namespace!

See the Breaking Changes guide for:

  • Namespace consolidation details
  • OpenTelemetry naming conventions (default in v4)
  • Migration paths and compatibility options

Run this checklist to confirm everything is working:

  • Package installed (dotnet list package shows it)
  • Project builds without errors (dotnet build)
  • IntelliSense recognizes using Purview.Telemetry;
  • Test interface compiles successfully
  • Generated DI extension method appears in IntelliSense
  • Application runs and telemetry is emitted
Version Release Date Notes
4.0.0-prerelease.2 2026-02-14 Added TelemetryNames generated classes
4.0.0-prerelease.1 2026-02-08 OTel-aligned naming across MELT
4.0.0-prerelease.0 2026-02-08 Namespace consolidation, OTel naming conventions
3.2.4 2025-07-27 Last stable v3 release

See the CHANGELOG for complete version history.