日期:2014-05-18  浏览次数:21025 次

C#虚构函数中可以这么干么?
为了获取执行过程中的错误,写出如下代码:
C# code
public class TestError
    {
        private StringBuilder error;
        public string Error
        {
            get
            {
                return error.ToString();
            }
           private set
            {
                Error = value;
            }
        }

        public TestError()
        {
            error = new StringBuilder();
        }
        ~TestError()
        {
            Error = error.ToString();
        }
        public void Fun1()
        {
           //干了很多事,其中会被return掉,每次return就会append一个error信息
            error.AppendFormat("Error1,{0}.{1}", "null", Environment.NewLine);
        }

        public void Fun2()
        {
            //干了很多事,其中会被return掉,每次return就会append一个error信息
            error.AppendFormat("Error2,{0}.{1}", "last error", Environment.NewLine);
        }
    }


fun1和fun2中分成好几个步骤执行,其中只要有执行失败的就return掉了,外部不能很方便的获取执行过程中的所有错误。
因此很龌蹉的写出了以上代码,在调用完成之后,获取属性Error的值,去获取错误信息。
我想问一下,这个操作在析构函数里面整会不会存在什么问题?

------解决方案--------------------
为啥不用 finally ,你这样析构函数根本调用不到。
------解决方案--------------------
楼主这么干确实太狠了,

C# code

//楼主以后可以这么干,把你想输出的错误记录下来,没必要那样干的
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Globalization;

/// <summary>
///ErrorUtil 的摘要说明
/// </summary>
public class ErrorUtil
{
    public ErrorUtil()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static void WriteError(string errorMessage)
    {
        try
        {
            string path = "~/Error/" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt";
            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
            {
                File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
            }
            using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
            {
                w.WriteLine("\r\n错误实例 : ");
                w.WriteLine("日期:{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                //string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +". Error Message:" + errorMessage;
                w.WriteLine(System.Web.HttpContext.Current.Request.Url.ToString());
                w.WriteLine(errorMessage);
                w.WriteLine("_________________________________________________________________________________");
                w.Flush();
                w.Close();
            }
        }
        catch (Exception ex)
        {
            WriteError(ex.Message);
        }
    }

}
还可以把这个方法放进这个文件Global.asax
  void Application_Error(object sender, EventArgs e) 
    {
        //在出现未处理的错误时运行的代码
        Exception objErr = Server.GetLastError().GetBaseException();
        string err = "错误明细:" + objErr.Message.ToString();

        ErrorUtil.WriteError(err);
        //Server.ClearError();
    }
//

------解决方案--------------------
如果放进全局文件中,会把所有的错误都记录下来

如果不想记录所有的,就想在哪个方法中调用都可以了,那样有错误就可以记录
------解决方案--------------------
探讨
为啥不用 finally ,你这样析构函数根本调用不到。

------解决方案--------------------
你起作用的是 get 不是析构。