• /
  • Log in
  • Free account

Custom instrumentation via attributes (.NET)

New Relic's .NET agent provides several options for custom instrumentation. Custom instrumentation allows you to instrument parts of your app that are not instrumented automatically. This document describes how to instrument your app by decorating the methods in your app code with attributes.

  • Use the Transaction attribute to create a custom transaction. You can also mark the custom transaction as a web transaction with the attribute's Web property.
  • Use the Trace attribute to add custom instrumentation to methods that are invoked within a preexisting transaction.

Requirements and recommendations

Requirements include:

  • .NET agent version 6.16.178.0 or higher.
  • You must be willing to modify your source code. If you cannot or do not want to modify your source code, use custom instrumentation via XML.
  • Your project must have a reference to NewRelic.Api.Agent.dll (for example, installing the package and placing using NewRelic.Api.Agent; in your code). This package is in the NuGet gallery.
  • The Transaction and Trace attributes must be applied to concrete implementations of methods. They cannot be applied on interfaces or super class method definitions.

Transactions called within transactions

Methods decorated with the [Transaction] attribute will only create a new transaction when one does not already exist. When a method decorated with [Transaction] is called from within a previously started transaction, it will be treated as the [Trace] attribute instead, and will provide more information about the existing transaction.

Create a new non-web transaction

To start a non-web transaction (also known as a background request) with the Transaction attribute:

[Transaction]
public void Run()
{
  // your background task
}

For details about why to use either web or non-web, see Classify as web or non-web.

Create a new web transaction

To tell the agent to mark a non-web task as a web browser transaction, use either of these options:

  • Set the Web property of the Transaction attribute to true.
  • Set the transaction's URI with SetTransactionUri().
[Transaction(Web = true)]
public void Run()
{
  var uri = new Uri("http://www.mydomain.com/path");
  NewRelic.Api.Agent.NewRelic.SetTransactionUri(uri);
  
  // your web task
}

When used inside a previously started transaction, this will be treated as a [Trace] attribute.

For details about why to use either web or non-web, see Classify as web or non-web.

Add detail to existing transactions with Trace

If your transaction traces show large blocks of un-instrumented time and you want to include additional methods within the trace, you can use the Trace attribute:

[Trace]
protected void MethodWithinTransaction()
{
  // your app code
}

Properties for [Transaction]

The Transaction attribute supports the following properties:

Read forum posts about instrumentation

For more specific recommendations, check out these posts in our Explorers Hub community:

Use other API functions

For more about the .NET agent API and its functionality, see New Relic's .NET agent API guide. For custom instrumentation without modifying your source code, see Create transactions via XML and Add detail to transactions via XML.

For more help

If you need more help, check out these support and learning resources:

Create issueEdit page
Copyright © 2021 New Relic Inc.