Introduction to .NET custom instrumentation

For supported frameworks, 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 doesn't have default instrumentation, 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 work that is not instrumented by default. For other ways of configuring your app's instrumentation, see Guide to using the .NET agent API.

For both agents (.NET Framework and .NET Core), custom instrumentation is done the same way, unless otherwise stated.

Choose a method of custom instrumentation

The .NET agent (both .NET Framework and Core) 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:

Web vs. non-web transactions

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

However, when you create a transaction via custom instrumentation, you must categorize it as web or 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.

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

For more help

Recommendations for learning more: