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"