Thursday, November 20, 2014

Enterprise Library 6 Exception Logging Tutorial

Hi everyone,

Since my last post it looks like the problems with EntLib6 and NuGet have been resolved so I wanted to give you all a simple tutorial on how to get exception logging working with your Visual Studio 2013 application.

Step 1: Install EntLib6 from NuGet package manager.


Step 2: Add reference for System.Configuration


Step 3: Setup the App.config


Add Application Settings
Add Configuration Sources
Add Exception Handling Settings
Add Logging Settings

Configure an Exception Handling Policy.  I used no post handling action "none"


Add a Handler


Make note of the logging Categories.  You can create your own but I simply used the General Category for this step by step guide.


Save the config.


Now you need to add some code to the Program.cs file to register a Logging Factory.  You can simply copy and paste the following code into your Main() method.

            IConfigurationSource source = ConfigurationSourceFactory.Create();

            LogWriterFactory logwriterFactory = new LogWriterFactory(source);
            var logWriter = logwriterFactory.Create();
            Logger.SetLogWriter(logWriter);

            var exceptionPolicyFactory = new ExceptionPolicyFactory(source);
            var exceptionManager = exceptionPolicyFactory.CreateManager();
            ExceptionPolicy.SetExceptionManager(exceptionManager);

            Application.ThreadException +=
                Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException +=
                CurrentDomain_UnhandledException;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

Now you have to add two additional methods in order to catch, handle and log Thread Exceptions and Unhandled Exceptions. Copy and paste the following below you Main() method.

         static void Application_ThreadException(object sender,                                   System.Threading.ThreadExceptionEventArgs e)
        {
            ExceptionPolicy.HandleException(e.Exception, "UI Policy");
            MessageBox.Show("An unexpected exception occurred"
                    + " and has been logged. Please contact support." + e.Exception.Message);

        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is Exception)
            {
                ExceptionPolicy.HandleException((Exception)e.ExceptionObject, "Unhandled Policy");
                MessageBox.Show("An unhandled exception occurred"
                    + " and has been logged. Please contact support.");

            }
        }



Finally you need to add a try and catch around some code.  Inside the catch add the following code.

                bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy");
                if (rethrow)
                {
                    throw;
                }
                MessageBox.Show("Error handled and logged to the event viewer. " + ex.Message, "Failure",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);


Now you can run and test it out!

NOTE; When you go to test this out for the first time, you will have to save your application and close it.  You then have to open Visual Studio 2013 as an administrator.  So right click the VS2013 shortcut and click runas administrator.  Now open your project and test it out.

What will happen is a MessageBox will alert you that an exception was caught and has been logged.  The way this example is setup the exceptions will be logged in the EventViewer.  To view the error log simply go to start, in the search bar type event viewer and open it.  Once you have opened the event viewer, expand the Windows Logs and click on Application.  You will see an Error with the Source, "Enterprise Library Exception Logging".

I hope this helps those who are looking for an exception logging solution.  Enterprise Library 6 is very nice.  There wasn't a lot of documentation out there when I first started using EntLib6 with Visual Studio 2013 so I had a somewhat difficult time getting it to work initially but once I did it has worked flawlessly.

Peace!