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.
Important
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: |
Don't create transactions for long-running methods
Don't use custom instrumentation (the [Transaction] attribute or an XML BackgroundThreadTracerFactory definition) to create a transaction on a method expected to run for an extended period, such as several minutes or hours. Common examples include:
- The
Mainmethod of a console application or service. - A worker loop that runs for the lifetime of the process, such as a
BackgroundService.ExecuteAsyncimplementation. - A scheduled or batch job (for example, a Quartz.NET
IJob.Executemethod or a Hangfire job) that processes many records in a single run.
Caution
The agent retains transaction data in memory until the transaction ends and reports that data to New Relic only after the transaction completes. A transaction that runs for hours, or never ends, causes the following issues:
- Transaction data doesn't appear in New Relic until the method returns. If the process exits or crashes first, the data might never appear.
- Memory usage grows while the transaction remains open, especially when the transaction makes many instrumented database, cache, or external calls. In some cases, this can lead to an
OutOfMemoryException. - The resulting transaction is a single, extended data point that doesn't provide useful metrics for measuring response time or throughput.
Instead, create a transaction for each discrete unit of work, such as a single loop iteration, queue message, or batch of records. Apply [Transaction] (or the XML transaction definition) to the method that handles that unit of work, and ensure the calling long-running method isn't instrumented as a transaction. If the outer method is instrumented as a transaction, the agent treats the inner method as a segment of the existing transaction rather than starting a new one. For an example, see Instrument a console app.
Classify as "web" or "non-web" (attribute method only)
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.
|