65.9K
CodeProject is changing. Read more.
Home

How To Make Exception Reporting in log4net Awesome

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Oct 14, 2014

LGPL3
viewsIcon

10371

How to make exception reporting in log4net awesome

Are you using log4net in an existing application and feel that you got no control over your errors? We’ve now created a seamless log4net integration. That means that we will automatically report all exceptions that you are logging.

Problem

In legacy applications, you typically have services that handle business logic like the one below, where all exceptions are logged.

public class SomeService
{
    private ILog _logger = LogManager.GetLogger(typeof (SomeService));

    public void DoSomeStuff()
    {
        try
        {
            //[...]
        }
        catch (Exception ex)
        {
            _logger.Error("Failed", ex);
        }
    }
}

The problem with that are that the errors are drowning in all other log noise. Until now, it has been hard to address that without having to rewrite all those log statements.

Solution

With our solution, you just have to add two lines of code to inject OneTrueError into the log4net pipeline:

class Program
{
    static void Main(string[] args)
    {
        XmlConfigurator.Configure(new FileInfo("log4net.config"));
        OneTrue.Configuration.Credentials("99948f8a-545d-491b-8dff-4fef6c250110",
                                            "c742290e-a80d-4ecb-8065-338780b61b2a");

        // injects into the log4net pipeline
        OneTrue.Configuration.CatchLog4NetExceptions();
            
        //the rest of your app initialization
    }
}

(You need to install our “onetrueerror.log4net” nuget package first.)

With that, you got all our features including the dead easy email notifications:

Signup at OneTrueError today! It’s free.