-
Notifications
You must be signed in to change notification settings - Fork 2
Sample Application
The sample app is a complete weather forecast application demonstrating the Purview Telemetry Source Generator in a real-world .NET Aspire setup. It covers all three telemetry targets — Activities, Logging, and Metrics — including multi-target methods that generate multiple telemetry types from a single method call.
The solution lives in samples/SampleApp/.
| Project | Description |
|---|---|
SampleApp.AppHost |
.NET Aspire orchestrator — wires up all resources and the Aspire dashboard |
SampleApp.APIService |
RESTful weather API backend; contains the primary telemetry interfaces |
SampleApp.Web |
Blazor Server frontend that calls the API and has its own HTTP client telemetry |
SampleApp.Shared |
Shared WeatherForecast DTO used by both API and frontend |
SampleApp.ServiceDefaults |
Common Aspire service defaults (OpenTelemetry, health checks, resilience) |
SampleApp.APIService.UnitTests |
Unit tests demonstrating how to mock and assert against generated telemetry interfaces |
There are three generated telemetry interfaces across the solution:
IEntityStoreTelemetry (SampleApp.APIService) — the interface from the Quick Start guide; shows the full multi-target pattern with Activity + Logging + Metrics on a single method:
[ActivitySource]
[Logger]
[Meter]
interface IEntityStoreTelemetry
{
[Activity]
[Info]
[AutoCounter]
Activity? GettingEntityFromStore(int entityId, [Baggage] string serviceUrl);
[Event]
[Trace]
void GetDuration(Activity? activity, int durationInMS);
[Context]
void RetrievedEntity(Activity? activity, float totalValue, int lastUpdatedByUserId);
[Warning]
void EntityNotFound(int entityId);
[Histogram]
void RecordEntitySize(int sizeInBytes);
}IWeatherServiceTelemetry (SampleApp.APIService.Services) — the primary telemetry for the weather service; demonstrates more advanced patterns including enumerable expansion and [ExcludeTargets]:
[ActivitySource]
[Logger]
[Meter]
public interface IWeatherServiceTelemetry
{
[Activity(ActivityKind.Client)]
[Trace]
Activity? GettingWeatherForecast([Baggage] string someRandomBaggageInfo, int requestedCount);
[Event]
void ForecastReceived(Activity? activity, int minTempInC, int maxTempInC);
[AutoCounter]
[Warning]
[Event]
void ItsTooCold(Activity? activity, int minTempInC, int tooColdCount);
[Histogram]
void HistogramOfTemperature(int temperature);
[Error]
[AutoCounter]
void RequestedCountIsOutOfRange(int requestCount);
}IWeatherAPIClientTelemetry (SampleApp.Web.Clients) — telemetry for the Blazor frontend's HTTP client; uses [ExcludeTargets] to prevent certain parameters appearing in specific telemetry targets:
[ActivitySource]
[Logger]
[Meter(InstrumentPrefix = "weather")]
public interface IWeatherAPIClientTelemetry
{
[Activity(ActivityKind.Client)]
[Info]
[AutoCounter]
Activity? GetWeatherForecasts(int? count);
[Event]
[Error]
[AutoCounter]
void FailedToGetForecast(Activity? activity, Exception ex,
[ExcludeTargets(Targets.Activities)] int? count);
[Event(ActivityStatusCode.Ok)]
[Debug]
void ForecastsRecieved(Activity? activity, int forecastCount,
[ExpandEnumerable(100), ExcludeTargets(Targets.Activities)] WeatherForecast[] weatherForecasts);
}The SampleApp.APIService.UnitTests project demonstrates how to unit test code that depends on generated telemetry. Since the generator works from interfaces, you simply mock the interface with any compatible mocking library:
// Using NSubstitute
static IWeatherServiceTelemetry CreateTelemetry() =>
Substitute.For<IWeatherServiceTelemetry>();
// Use the mock in the system under test
var sut = new WeatherService(telemetry: CreateTelemetry(), ...);Tests are split across WeatherServiceTests.Success.cs, WeatherServiceTests.Failure.cs, and WeatherServiceTests.Validation.cs.
- Open the solution (
samples/SampleApp/SampleApp.slnx) and run theSampleApp.AppHostproject. - The .NET Aspire dashboard opens automatically — it shows all resources:
api-service,web, and a dedicatedscalarresource. - To generate telemetry, use one of two approaches:
-
Web Frontend: Click the
webresource endpoint to open the Blazor app, navigate to the Weather page, and click Load Weather (or trigger error scenarios). -
Scalar API Docs: Click the
scalarresource endpoint to open the Scalar UI, then test the API endpoints directly.
-
Web Frontend: Click the
| Step | Image |
|---|---|
The Aspire dashboard shows all resources — api-service, web, and scalar. Click any endpoint link. |
![]() |
| In the Scalar UI, scroll to the endpoint you want to test and click Test Request. | ![]() |
Press the (Play) button several times to generate telemetry data. Use error scenarios to see failure paths. |
![]() |
The resulting telemetry appears in the various sections of the Aspire dashboard:
| Dashboard view | Image |
|---|---|
| Failed requests visible in the resource list. | ![]() |
| Console logs for the running service. | ![]() |
| Structured logs with full property context. | ![]() |
| Distributed traces across the request path. | ![]() |
Trace detail showing Activity spans, ActivityEvents, and baggage/tag properties. |
![]() |
| Metrics dashboard with counters and histograms. | ![]() |
| Histogram distribution view. | ![]() |
The sample projects have <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> enabled, so generated files appear in your IDE under:
SampleApp.APIService/
obj/Release/net10.0/generated/
Purview.Telemetry.SourceGenerator/
Purview.Telemetry.SourceGenerator.TelemetrySourceGenerator/
SampleApp.APIService.WeatherServiceTelemetryCore.Activity.g.cs
SampleApp.APIService.WeatherServiceTelemetryCore.Logging.g.cs
SampleApp.APIService.WeatherServiceTelemetryCore.Metric.g.cs
SampleApp.APIService.WeatherServiceTelemetryCoreDIExtension.DependencyInjection.g.cs
SampleApp.APIService.TelemetryNames.g.cs
See Generated Output for full annotated examples of the generated code.
Important
Consider helping children around the world affected by conflict. You can donate any amount to War Child here - any amount can help save a life.
Purview Telemetry Source Generator v4.0.0-prerelease.1 | Home | Getting Started | FAQ | Breaking Changes | GitHub


(






