程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 用PostSharp來坐異常處理

用PostSharp來坐異常處理

編輯:C#入門知識

代碼如下:

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("參數信息"); 
                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("異常信息:"); 
                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); 
            } 
        } 
    } 

 作者“dz45693的專欄”

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved