Add detail to transactions via XML (.NET)

New Relic collects and reports information on web browser transactions and background tasks. New Relic normally produces complete information right out of the box without any need 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. New Relic's .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.

Add custom instrumentation

Extension files define a number of tracer factories in an instrumentation element. Each tracer factory contains match elements which define the assembly, fully qualified class name, and method name to match. To define a new custom instrumentation XML file:

  1. Create a new .xml file in the extensions directory. The .NET agent reads every XML file in this extensions directory to define its instrumentation set. The file is located in:

    C:\ProgramData\New Relic\.NET Agent\Extensions

    Do not put the .xml file in the C:\Program Files\New Relic\.NET agent\Extensions directory. The file must be in the location above for custom instrumentation to work.

  2. Using an XML-aware editor such as Visual Studio, edit your custom instrumentation file to specify the methods you want to instrument. Use the tracer definitions in the distributed XML files and the sample as an example.

    Do not modify the distributed xml files. These files are overwritten whenever the agent is upgraded.

  3. Validate the .xml instrumentation file against extension.xsd.
  4. 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.

  5. 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.

  6. If your app is IIS-hosted, restart IIS.

  7. For non-IIS applications, restart your application's host process or the application itself.

Ignore a transaction

You can stop a transaction from being reported via a custom instrumentation file. Whenever an ignored method is called, the .NET agent ignores the entire parent transaction. This is the same as calling IgnoreTransaction(). To do so, add custom instrumentation, and add a tracerFactory whose name is NewRelic.Agent.Core.Tracer.Factories.IgnoreTransactionTracerFactory:

<tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.IgnoreTransactionTracerFactory">
  <match assemblyName="System.Web.Extensions" className="System.Web.Handlers.ScriptResourceHandler">
   <exactMethodMatcher methodName="Throw404" />
  </match>
</tracerFactory>

Example MyInstrumentation.xml

This example instruments two methods and ignores another method:

  • Instrument: CustomInstrumentDemo.Controllers.FirstController.FirstExample()
  • Instrument: CustomInstrumentDemo.Controllers.SecondController.SecondExample()
  • Ignore: CustomInstrumentDemo.Controllers.SecondController.ThirdExample()
  • Instrument: CustomInstrumentDemo.Controllers.SecondController.FourthExample(int id, string name)

To implement this custom instrumentation scheme, use the following example file and instrumented methods:

Example custom instrumentation file

The following is an example custom instrumentation file named MyInstrumentation.xml:

<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
  <instrumentation>
    <!-- instrument CustomInstrumentDemo.Controllers.FirstController.FirstExample and creates metric named Custom/Foo1 -->
    <tracerFactory metricName="Custom/Foo1">
      <match assemblyName="CustomInstrumentDemo" className="CustomInstrumentDemo.Controllers.FirstController">
        <exactMethodMatcher methodName="FirstExample" />        
      </match>
    </tracerFactory>
     <!-- instrument CustomInstrumentDemo.Controllers.SecondController.SecondExample -->
    <tracerFactory>
      <match assemblyName="CustomInstrumentDemo" className="CustomInstrumentDemo.Controllers.SecondController">
        <exactMethodMatcher methodName="SecondExample" />
      </match>
    </tracerFactory>
    <!-- ignore CustomInstrumentDemo.Controllers.SecondController.ThirdExample -->
    <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.IgnoreTransactionTracerFactory">
      <match assemblyName="CustomInstrumentDemo" className="CustomInstrumentDemo.Controllers.SecondController">
        <exactMethodMatcher methodName="ThirdExample" />
      </match>
    </tracerFactory>
    <!-- instrument CustomInstrumentDemo.Controllers.SecondController.FourthExample and creates metric named Custom/{value},
         where {value} is the value of the first string parameter of the instrumented method
         (in this case, the first string parameter is "name"). -->
    <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.CustomSegmentTracerFactory">
      <match assemblyName="CustomInstrumentDemo" className="CustomInstrumentDemo.Controllers.SecondController">
        <exactMethodMatcher methodName="FourthExample" />
      </match>
    </tracerFactory>
  </instrumentation>
</extension>
Example methods to be instrumented

This code contains the three methods specified by the custom instrumentation file above:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading;

namespace CustomInstrumentDemo.Controllers
{

    public class FirstController : Controller
    {
    
        public ActionResult Foo1()
        {
            ViewBag.Message = "Your Sample Page";

            FirstExample();
            
            return View();
        }

        public void FirstExample()
        {
            int firstVar = 1;
 
            for (int i = 0; i <= 500; i++)
                {
                    firstVar *= 5;
                    Thread.Sleep(50);
                } 

        }
    }

    public class SecondController : Controller
    {

        public ActionResult Foo2()
        {
            ViewBag.Message = "This view will include detailed information on the SecondExample method";

            SecondExample();

            return view;
        }

        public ActionResult Foo3()
        {
            ViewBag.Message = "This view will be ignored because of the inclusion of the ThirdExample method";

            ThirdExample();
        }

        public void SecondExample()
        {
            int secondVar = 2;

            using (var connection = new SqlConnection(ConnectionStrings["MsSqlConnection"].ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("SELECT * FROM table", connection))
                using (var reader = command.ExecuteReader())
                {
                    reader.Read();
                }
            }

        }

        public void ThirdExample()
        {
            try
            {
                var ImNotABool = "43";
                bool.Parse(ImNotABool);
            }
            catch (Exception ex)
            {
                NewRelic.Api.Agent.NewRelic.NoticeError(ex);
            }

        }

        public void FourthExample(int id, string name)
        {
            Console.WriteLine("The ID passed in was " + id + " and the name passed in was " + name);
        }
    }
}

Name metrics

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 metricName with Custom/ (for example, metricName="Custom/OrderSubmissions").

<!-- 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>

Name transactions

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. Use the transactionNamingPriority attribute to tell the agent to give a tracer transaction naming priority.

Valid values are 1 to 7, where 7 takes precedence over 1 to 6. Also, the metricName attribute must begin with Custom/ (for example, metricName="Custom/instance").

<!-- 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>

XSD validation

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.

Troubleshooting

The .NET agent will write a log message to NewRelic.Profiler.####.log (where #### 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.

For more help

Additional documentation resources include:

Recommendations for learning more: