To continue on from Part 2. For this article we will talk about one of the aspects I utilize the most OnExceptionAspect
Note: This may be the last generic post, I am working on a new blog series will show off using PostSharp with some really good new stuff in a real world style application.
Setup
- Windows 7 X64 (Last post using this, I am switching to Window 8)
- Visual Studio 2010 (Last post using this, I am switching to VS 11)
- PostSharp 2.1.6.4
- PostSharp-Aspects
For this example we are going to make an aspect that captures and logs the exception in a format of our choosing. (you can follow along by looking at ExceptionHandlerAttribute.cs in PostSharp-Aspects .
First thing we will do is created a new class, for this demo we will name it ExceptionHandlerAttribute.cs. Once it is created we will inherit from OnExceptionAspect. Once Complete it should look like the following:
[Serializable]
public class ExceptionHandlerAttribute : OnExceptionAspect
The next thing we will do is override the OnException method and pass in a parm of type MehodExecutionArgs. It should looks similar to this:
[Serializable]
public class ExceptionHandlerAttribute : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
}
}
For good measure we will format our exception how we want to view it. My personal preference I like to divide up messages on lines and separate (in case it happens) multiple errors with identifiers. I also prefer is the InnerException exist (sometime it does not) to log that also. Because it is good practice I also log the name of the method where the exception happened and the time.
As an added bonus if you are using a UI (I will show this in a later posting) you can throw a new exception that will bubble up to the client that can say something as simple as “There was an error logged, please contact your administrator”. No need for the end user to see exception information.
So our Aspect should now look similar to this:
[Serializable]
public class ExceptionHandlerAttribute : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
string messageOut = string.Format(
“\n\n ———-{0} had an error ———\n@ {1}:\n\n {2}\n\nStackTrace:{3}”,
args.Method.Name,
DateTime.Now,
args.Exception.Message,
args.Exception.StackTrace);
if (args.Exception.InnerException != null)
{
messageOut += string.Format(“\n ———- InnerException ——–\n : {0}”, args.Exception.InnerException);
}
Console.WriteLine(messageOut);
// throw new Exception(“There was an error logged, please contact your administrator”);
}
}
Using the example from Part 2, we will add the wrapper [ExceptionHandler] and throw a new exception in the method our output will look like this:
I am leaning towards Codeplex to release my examples soon. I have a deep love for TFS so when I finish the details I will announce them. As always any insight or options are greatly appreciated.
Continued shout out: PostSharp team has created a collection of ready-made aspects in the PostSharp ToolKit. Please visit and read about it, some great stuff.