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 handles 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:

notifications

Signup at OneTrueError today! It’s free.

  • eternalisresearch

    Being new to both log4net and One True Error, I’m a bit of a loss as to what log4net.config file would contain if all I want to do is send exceptions to One True Error. Do you have a complete example of how this might be integrated?

    Also, 95% of the links to the blog.onetrueerror.com pages are returning only whitespace (no content).

    Thanks!

    • Apologies for the late reply.

      1. The log4net adapter is typically only for those who have been using log4net to log exceptions. It’s just a way to get all those exceptions without having to rewrite the current code. If you have not used log4net previously I suggest that you use one of our other nuget packages.

      2. We’re running the blog in Azure and Microsofts host package for wordpress/PHP is flawed. The WinCache module crashes and I’ve tried to get them to fix it. Expect the blog to be moved from Azure.