简体   繁体   中英

How to manually push errors to NewRelic in .NET

This question covers how to manually notice errors in NewRelic in Ruby.

I need to do the same thing in C#.

How can I manually send errors to NewRelic in .NET?

According to NewRelic's .NET Api , you can do this with

NewRelic.Api.Agent.NewRelic.NoticeError(ex);

where ex is an Exception .

In order to do this across all WebApi as an ExceptionLogger to log errors into New Relic

Add the following class to your project

public class NewRelicExceptionLogger : ExceptionLogger
{
    public override void Log(ExceptionLoggerContext context)
    {
        NewRelic.Api.Agent.NewRelic.NoticeError(context.ExceptionContext.Exception);
    }
}

and register it Global.asax.cs under Application_Start() :

protected void Application_Start()
{
    //...
    GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger), new NewRelicExceptionLogger());

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM