New Relic's .NET agent reports handled errors as though they are standard errors. This is most common with Azure worker roles, console apps, async work, and similar operations.
Solution
To avoid false error reports, instrument a method that directly or indirectly contains the exception handler. Instrument the target method by defining a custom instrumentation file, or by wrapping the method in a custom transaction, as shown in this example:
In this example, New Relic reports an error from GetResponse()unless the method Foo() is instrumented. As long is Foo is instrumented, New Relic begins a transaction when Foo is called and ends the transaction when Foo ends.
Because the error is handled before Foo ends, New Relic will not report an error. Note also that GetResponse() becomes a segment of the Foo transaction.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingSystem.Net;
usingSystem.IO;
namespaceErrorTester
{
classProgram
{
staticvoidMain(string[] args)
{
var i =0;
while(true)
{
Foo(++i);
}
}
staticvoidFoo(int i)
{
try
{
GetNotFound();
}
catch(Exception ex)
{
Console.WriteLine("Got it "+ i +"!");
Thread.Sleep(1000);
}
}
staticstringGetNotFound()
{
string uri ="https://localhost/Test/this/is/not/a/real/page";
var request =(HttpWebRequest)WebRequest.Create(uri);
var response = request.GetResponse();
var data =newStreamReader(response.GetResponseStream()).ReadToEnd();
The only errors New Relic's .NET agent reports are unhandled errors that end a transaction. If your app calls an exception handler before the transaction ends, New Relic will not report an error.
However, New Relic does not always detect exception handlers when the error occurs outside of a web transaction, WCF transaction, or custom transaction. This is because the agent creates "mini-transactions" for instrumented methods that are not associated with a transaction.
When the instrumented method exits, the mini-transaction ends. If the mini-transaction throws an error and the instrumented method does not handle it, then New Relic will report an error.
You can see this in a console app that calls GetResponse(), as shown in the example. If GetResponse throws an error, then New Relic will report it, even though GetResponse() is called within a try/catch block. The agent reports an error because the GetResponse() "mini-transaction" ended and the error was still unhandled on transaction exit.