Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / logging

How To Make Exception Reporting in log4net Awesome

0.00/5 (No votes)
15 Oct 2014LGPL3 10.4K  
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.

C#
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:

C#
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.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)