Introduction to .NET custom instrumentation

For fully supported frameworks (.NET Framework | .NET Core), the New Relic .NET agent automatically collects and reports information on web transactions and browser tasks. However, if you are using an application framework that isn't automatically instrumented (or not using a framework) you may see large blocks of time in the New Relic UI lacking detail, or you may not see any transactions. In this case, you can use custom instrumentation to add transactions and detail.

This document describes how to instrument activity that is not instrumented by the .NET agent by default. For other ways of configuring your app's instrumentation, see Guide to using the .NET agent API.

For both of the agent's supported frameworks (.NET Framework and .NET Core), custom instrumentation is done the same way, unless otherwise stated.

Choose a method of custom instrumentation

The .NET agent supports two methods of custom instrumentation:

Method Description
Attribute custom instrumentation

Instrument your code by decorating your methods with an attribute. Attribute instrumentation is simpler to implement than XML instrumentation because you only need to add a single decorator, in the same place in your code as the method you want to instrument.

For instructions, see Custom instrumentation via attributes.

XML custom instrumentation

Instrument your code by listing the target methods in an XML file. XML instrumentation is more complex than attribute instrumentation, and it will fail if you change the name of the assembly, class, or method you want to instrument. However, XML instrumentation does not require you to modify your source code.

For instructions, see Create transactions via XML and Add detail to transactions via XML.

Creating transactions vs. adding detail

When you add custom instrumentation via either method, you need to choose between creating a new transaction and adding instrumentation to an existing transaction:

Situation Recommendation
The method you want to instrument does not appear in the New Relic UI at all

Create a new transaction. For details, see:

The transaction appears in the New Relic UI but contains sections of uninstrumented time

Add detail to an existing transaction. For details, see:

Classify as "web" or "non-web" (attribute method only)

New Relic APM separates transactions into web transactions and non-web transactions in the UI. When you add detail to an existing transaction, its category is determined by the category of the parent transaction.

When you create a transaction via custom instrumentation using the attribute method, you must categorize it as web or non-web. (This is not possible with XML instrumentation; all XML-created transactions are classified as non-web.)

Category When to use
Web Use web for web requests. You can only mark new transactions as web transactions with custom instrumentation using attributes, not by using XML.
Non-web

Use non-web for other types of background requests, such as console apps and services.

For more help

Recommendations for learning more: