Generic success and failure logging using the StackTrace class

by Heathesh 31. October 2010 19:59

Haven't you ever wished you could log success and failures without having to code separate methods? Well I thought about a simple way of solving this problem, and came up with the following. The method will allow you to log the success or failure of any method without needing to pass different parameters etc. You can use it to implement generic audit logging for your applications.

To start off with, you'll need the following usings on whichever class you add the method:

using System.Diagnostics;
using System.Reflection;

Next, add the method itself:

        /// <summary>
        /// Log an event
        /// </summary>
        /// <param name="ex"></param>
        private static void log(Exception ex = null)

            StackTrace stackTrace = new StackTrace();
            MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
            string serviceName = methodBase.ReflectedType.FullName;
            string method = methodBase.Name;

            //you can off course implement your own email / file logging etc. type methods here
            Console.WriteLine("Full Name: {0}.{1}", serviceName, method);
            Console.WriteLine("Log type: {0}", ex == null ? "Success" : "Exception");

            if (ex != null)
                Console.WriteLine("Exception: {0}", ex.Message);


The basics of the method is that it uses the StackTrace to determine the calling method's details, and depending on whether or not an exception is passed in it can be used to log success or failure. Here is an example of a successful method:

        /// <summary>
        /// A method that succeeds
        /// </summary>
        private static void thisMethodSucceeds()
            //do whatever you want

            //now log that it succeeded

The output of this method will be (Serialization.Program is the name of my application):

Full Name: Serialization.Program.thisMethodSucceeds
Log type: Success

Here is an example of a method that fails. Notice we simply pass the exception into the log method now:

        /// <summary>
        /// A method that fails
        /// </summary>
        private static void thisMethodFails()
                //try and do something, here we're going to throw an exception to simulate something going wrong
                throw new Exception("An exception occured here");
            catch (Exception ex)
                //log the exception

                //NB: always "throw" an exception like this so as to maintain the stack trace, NEVER EVER "throw ex" as you will loose the precise
                //location the exception occured

            //if we get here the method was successful

The output of this failing method would be:

Full Name: Serialization.Program.thisMethodFails
Log type: Exception
Exception: An exception occured here

Now obviously you would need to change the "log" method above to do whatever logging you actually wanted i.e. log to file, email out, save to a database table. I just wanted to get you onto the right foot in terms of starting a logging mechanism that can be used throughout an application with minimal effort.

The only drawback is that you cannot log the actual parameter values passed into the methods (if there are any). By changing the "log" method and including "methodBase.GetParameters()" you can get the ParameterInfo[] array of parameters. However that only allows you to see the names and types of parameters, but not the actual values, which as far as I know might not be possible at all.

Happy logging!


Development | .Net | Visual Studio 2010 | VS2010

Comments are closed

Powered by BlogEngine.NET (with enhancements by Heathesh)
Theme by Mads Kristensen (with tweeks by Heathesh)


Microsoft Certified Professional

Microsoft Certified Technology Specialist

Answer Questions


Tag cloud


<<  July 2017  >>

View posts in large calendar