Use PostSharp to handle exceptions

Source: Internet
Author: User

The Code is as follows:

View plaincopy to clipboardprint? Class Program
{
Static void Main (string [] args)
{
String filePath = @ "C: \ log.txt ";
 
Trace. Listeners. Add (new LogTraceListener (filePath ));
Trace. Listeners. Add (new TextWriterTraceListener (Console. Out ));
 
MyClass my = new MyClass ();
Int ret = my. MyMethod (44, "asdf qwer 1234", 3.14f, true );
Console. WriteLine (ret. ToString ());
Console. ReadKey ();
}
}
Public class MyClass
{
Public MyClass ()
{
}
# If DEBUG
[HandleException]
# Endif
Public int MyMethod (int x, string someString, float anotherFloat, bool theBool)
{
Int B = 0;
Int a = 5/B;
Return x + 1;
}
}
[Serializable]
Public class HandleExceptionAttribute: MethodInterceptionAspect
{
Public override void OnInvoke (MethodInterceptionArgs args)
{
Try
{
Base. OnInvoke (args );
}
Catch (Exception ex)
{
Trace. WriteLine (string. Format ("{2} ---- Entering {0}. {1}.", args. Method. DeclaringType. Name,
Args. Method. Name, DateTime. Now. ToString ()));
Trace. WriteLine ("parameter information ");
For (int x = 0; x <args. Arguments. Count; x ++)
{
Trace. WriteLine (args. Method. GetParameters () [x]. Name + "=" + args. Arguments. GetArgument (x ));
}
Trace. WriteLine ("ReturnValue =" + args. ReturnValue );
Trace. WriteLine ("exception information :");
While (null! = Ex)
{
Trace. WriteLine ("Message =" + ex. Message );
Trace. WriteLine ("StackTrace =" + ex. StackTrace );
Ex = ex. InnerException;
}
 
}
}
}
 
Public sealed class LogTraceListener: TraceListener
{
Public string FileName {set; get ;}
Public LogTraceListener (string filename)
{
If (File. Exists (filename ))
{
FileStream fs = File. Create (filename );
Fs. Close ();
}
FileName = filename;
}
Public override void Write (string message)
{
Using (StreamWriter sw = new StreamWriter (FileName, true, Encoding. UTF8, Int16.MaxValue ))
{
Sw. Write (message );
}
}
 
Public override void WriteLine (string message)
{
Using (StreamWriter sw = new StreamWriter (FileName, true, Encoding. UTF8, Int16.MaxValue ))
{
Sw. WriteLine (message );
}
}
}

Author's "dz45693 column"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.