1.8 KiB
Manually instrument a .NET application
The automatic instrumentation provides a base you can build on by adding your own manual instrumentation. By using both automatic and manual instrumentation, you can better instrument the logic and functionality of your applications, clients, and frameworks.
To create your custom traces manually, follow these steps:
-
Add the
System.Diagnostics.DiagnosticSourcedependency to your project:<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="6.0.0" /> -
Create an
ActivitySourceinstance:private static readonly ActivitySource RegisteredActivity = new ActivitySource("Examples.ManualInstrumentations.Registered"); -
Create an
Activity. Optionally, set tags:using (var activity = RegisteredActivity.StartActivity("Main")) { activity?.SetTag("foo", "bar1"); // your logic for Main activity } -
Register your
ActivitySourcein OpenTelemetry.AutoInstrumentation by setting theOTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCESenvironmental variable. You can set the value to eitherExamples.ManualInstrumentations.Registeredor toExamples.ManualInstrumentations.*, which registers the entire prefix.
You can see a sample console application with automatic (HttpClient) and manual
instrumentation here.
Note that an
Activitycreated forNonRegistered.ManualInstrumentationsActivitySourceis not handled by the OpenTelemetry Automatic Instrumentation.
Further reading: