forked from 47-studio-org/PostSharp.Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReportAndSwallowExceptionAttribute.cs
50 lines (47 loc) · 2.09 KB
/
ReportAndSwallowExceptionAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using PostSharp.Aspects;
using PostSharp.Aspects.Dependencies;
using PostSharp.Serialization;
using System;
using System.Text;
namespace PostSharp.Samples.ExceptionHandling
{
/// <summary>
/// Aspect that, when applied to a method, reports and then swallows (ignores) any exception that this method may
/// throw.
/// The aspect also prints the data collected by the <see cref="AddContextOnExceptionAttribute" /> aspect.
/// </summary>
/// <remarks>
/// <para>
/// Do not indiscriminately apply this aspect to all methods as exceptions are generally useful. Only use apply
/// this aspect
/// to thread entry points or event handlers.
/// </para>
/// </remarks>
// Specify that this aspect must orderd after AddContextOnExceptionAttribute.
[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.After,
typeof(AddContextOnExceptionAttribute))]
[PSerializable]
public sealed class ReportAndSwallowExceptionAttribute : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
// Write the default exception information.
Console.WriteLine("Exception information");
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine(args.Exception.ToString());
Console.WriteLine("--------------------------------------------------------------");
var additionalContext = (StringBuilder) args.Exception.Data["Context"];
// Write the additional information that was gathered by AddContextOnExceptionAttribute.
if (additionalContext != null)
{
Console.WriteLine("Additional context information (call stack with parameter values)");
Console.WriteLine("--------------------------------------------------------------");
Console.Write(additionalContext.ToString());
Console.WriteLine("--------------------------------------------------------------");
}
// Ignore the exception.
Console.WriteLine("*** Ignoring the exception ***");
args.FlowBehavior = FlowBehavior.Continue;
}
}
}