我做的是winform开发。上面要我在程式中添加log4net,我添加好了。然后要求我用log4net记录所有form中catch到的Exception,并且不要一个个form的改。
我在网上找到用以下代码可以获得未被catch到的Exception:
        [STAThread]
        static void Main()
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
      
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
        }我要获得已经try catch的Exception该怎么做?
程式里所有的form都是继承的自定定义的BaseForm,能不能在这里做文章?

解决方案 »

  1.   

    我所有的Form中都有
    try{}
    catch(Exception err)
    {
    MessageBox.Show(err);
    }
    现在要用log4net记录下所有的Exception
    如果只有一个的话只需在catch中添加
    catch(Exception err)
    {
    log.Info(err);
    }
    但现在窗体很多,每个窗体的代码中又有很多try catch,一个个改太耗费时间,有没有省便的方法?
      

  2.   

    在网上找到这段代码:
    AppDomain.CurrentDomain.FirstChanceException += new EventHandler<FirstChanceExceptionEventArgs>(CurrentDomain_FirstChanceException);
            try
            {
                throw new Exception("test");
            }
            catch
            {
            }
            return;
    但FirstChanceException根本点不出来,有么有人知道为什么?
      

  3.   

    FirstChanceExceptionEventArgs 应该是自己写的,没抄全
      

  4.   

    这是我网上找到的比较全的代码:
    public event EventHandler<FirstChanceExceptionEventArgs> FirstChanceException;
    从注释上已经可以看到,该事件可以让我们尝到最新鲜、最及时的异常。下面是我写的一个Demo例子,来观察一下它的行为。首先是在main函数中对currentDomain注册FirstChanceException事件的处理方法。        static void Main(string[] args)
            {
                AppDomain.CurrentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>
    (CurrentDomain_FirstChanceException);
                SimpleExceptionTest.Test();
                Console.ReadLine();
            }        static void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
            {
                Console.WriteLine("CurrentDomain_FirstChanceException:");
                Console.WriteLine(e.Exception);
                Console.WriteLine();
                Console.WriteLine();
            }
    然后我在这个网址:http://msdn.microsoft.com/zh-cn/library/1dw188c1(v=VS.100).aspx
    查找AppDomain的事件,发现只有.NET Framework 3.5以上的版本才有FirstChanceException事件,而我的程式是VS2005开发的,根本无法用这个方法,我总不能叫主管把程式的解决方案升级到VS2010吧。 
      

  5.   

    如果需求中没有说要用对话框显示,就不要写messagebox这类代码。
      

  6.   

    这个怎样都好啦,问题是我怎么得到所有catch(Exception err)中的err