New Relic collects and reports information on web browser transactions and non-web transactions (background tasks). Normally the .NET agent produces complete information automatically, without any need for you to modify your application code. However, if New Relic does not support your framework, you may need to add custom instrumentation.
New Relic uses method tracers to implement custom instrumentation. A method tracer is a software probe that you can put on a method of any class. The .NET agent loads the instrumentation directives that define which methods should be traced from all XML files in the extensions directory.
This document describes instrumenting work when the agent is already collecting transactions, but you want to add more detail to those transactions. To instrument work when the agent isn't creating transactions automatically, see Create transactions via XML. You can also add detail and create transactions by decorating your code with attributes.
Extension files define a number of tracer factories in an instrumentation element. Each tracer factory contains match elements that define the assembly, fully qualified class name, and method name to match. To define a new custom instrumentation XML file:
Create a new
.xmlfile in the extensions directory used by the .NET agent to read every XML file and define its instrumentation set. For the .NET Framework agent, use the following location:
C:\ProgramData\New Relic\.NET Agent\Extensions
Do not put the .xml file in the
C:\Program Files\New Relic\.NET agent\Extensionsdirectory. The file must be in the
ProgramDatafile location for custom instrumentation to work.
Do not modify the distributed xml files. These files are overwritten whenever the agent is upgraded.
Copy this template into the file you created use the sample as an example. This instruments two methods but you can add methods to your instrumentation file.
The values AssemblyName, NameSpace.ClassName, and MethodName are case sensitive.
<?xml version="1.0" encoding="utf-8"?> <extension xmlns="urn:newrelic-extension"> <instrumentation> <!-- These methods appear in the transactions breakdown table and in transaction traces --> <tracerFactory metricName="Category/Name"> <match assemblyName="AssemblyName" className="NameSpace.ClassName"> <exactMethodMatcher methodName="MethodName" /> </match> </tracerFactory> <tracerFactory metricName="Category/Name2"> <match assemblyName="AssemblyName" className="NameSpace.ClassName2"> <exactMethodMatcher methodName="MethodName2" /> </match> </tracerFactory> </instrumentation> </extension>
.xmlinstrumentation file against
Non-IIS apps (such as a console app or background process): You must also create transactions via XML to contain the methods you instrument. Custom methods instrumented outside of a transaction will not be reported to New Relic.
If your .NET agent version is 7.0 or higher, this is the end of the procedure.
If your .NET agent version is lower than 7.0, continue to the next steps.
If your app is IIS-hosted, restart IIS.
For non-IIS applications, restart your application's host process or the application itself.
You can stop a transaction from being reported by using a custom instrumentation file. Whenever an ignored method is called, the .NET agent ignores the entire parent transaction. This is the same as calling
To ignore a transaction:
- Add custom instrumentation.
- Add a
tracerFactorywhose name is
<tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.IgnoreTransactionTracerFactory"> <match assemblyName="System.Web.Extensions" className="System.Web.Handlers.ScriptResourceHandler"> <exactMethodMatcher methodName="Throw404" /> </match> </tracerFactory>
In some cases, asynchronous work can be tracked as a separate transaction by applying the
<tracerFactory name="AsyncForceNewTransactionWrapper"> <match assemblyName="AssemblyName" className="Namespace.ClassName"> <exactMethodMatcher methodName="MethodName" /> </match> </tracerFactory>
Async usage considerations
The instrumented method must be invoked using
The instrumented method is not required to be
The instrumented method cannot have attribute instrumentation applied to it. It cannot be decorated with the
This example instruments two methods and ignores another method:
CustomInstrumentDemo.Controllers.SecondController.FourthExample(int id, string name)
To implement this custom instrumentation scheme, use the following example file and instrumented methods:
Metrics created from tracers will be named using the class name and method name of the matched method. You can override this name with the
metricName attribute. Begin
Custom/ (for example,
<!-- instruments MyCompany.Order.Submit() and creates a metric named Custom/OrderSubmissions --><tracerFactory metricName="Custom/OrderSubmissions"><match assemblyName="MyCompany" className="MyCompany.Order"><exactMethodMatcher methodName="Submit" /></match</tracerFactory>
The agent names transactions using the tracer in the transaction with the highest naming priority.
- Web transactions may be named using the HTTP handler, ASP name, MVC controller name, or web service name, depending on the tracers invoked in the transaction's execution.
- Background transactions with no tracer that explicitly names the transaction are rolled up into a single transaction name.
transactionNamingPriority attribute to tell the agent to give a tracer transaction naming priority. Valid values are
7 takes precedence over
6. Also, the
metricName attribute must begin with
Custom/ (for example,
<!-- instructs the agent to create a metric for MyControllerBase.Execute and to name the transaction using this tracer's metric name --><tracerFactory metricName="Custom/instance" transactionNamingPriority="7"><match assemblyName="MyCompany" className="MyCompany.MyControllerBase"><exactMethodMatcher methodName="Execute" /></match></tracerFactory>
The XML instrumentation file can be checked against the XSD file (located at
C:\ProgramData\New Relic\.NET Agent\Extensions\extension.xsd) with any XSD validator.
The .NET agent will write a log message to
#### is the PID of the instrumented process) as it rewrites methods. This helps to verify that custom instrumentation is being read and that the proper methods are being instrumented.
Avoid using the
name attribute of the
tracerFactory element in custom instrumentation. For more information, review the
extension.xsd schema file in the agent directory.