WPF程序启动崩溃:如何调试?

时间:2011-10-19 03:54:05

标签: c# wpf debugging

我有一个在开发PC和客户端PC上运行良好的WPF程序。但是在客户端PC 2上,它会在启动时立即崩溃并发送到Microsoft窗口。我希望得到一些关于如何追查错误的建议。这是我尝试过的:

  1. 在我的主窗口类中插入了try-catch:

    public MainWindow()
    {
      try
      {
         MessageBox.Show("Before InitComp()");
         InitializeComponent();
         MessageBox.Show("Before Sub1()");
         Subroutine1();
         MessageBox.Show("Before Sub2()");
         Subroutine2();
         ... etc ...
      }
      catch (Exception ex)
      {  ... code for MessageBox display error here ... }
    }
    
  2. 我们的想法是尝试隔离启动序列的哪个部分正在崩溃,但是第一个调试消息“在InitComp()之前”甚至没有出现。因此,即使在开始我的代码之前,应用程序似乎也崩溃了。

    1. 一种可能性是将整个VS2008安装在客户端PC 2中,加载源并使用IDE调试器来跟踪问题。这可能是发现问题最有效的方法。但我不想这样做因为a)客户端PC 2不属于我,b)它不能扩展:我必须为客户端PC 3/4/5 / ...做同样的事情,并且c)它违反了我的公司的VS2008许可证。
    2. 我应该如何调试此问题?

4 个答案:

答案 0 :(得分:4)

在App.xaml标题中,添加:

<Application DispatcherUnhandledException="App_DispatcherUnhandledException" />

并在App.xaml.cs中添加如下内容:

    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
    {
        log.Fatal("An unexpected application exception occurred", args.Exception);

        MessageBox.Show("An unexpected exception has occurred. Shutting down the application. Please check the log file for more details.");

        // Prevent default unhandled exception processing
        args.Handled = true;

        Environment.Exit(0);
    }

答案 1 :(得分:3)

旧学校方法:像这样的硬崩溃可能会冒泡到你可以通过Windows中的事件查看器看到的东西。你在那里检查了吗?很多时候,这给了我答案,没有任何额外的麻烦。

答案 2 :(得分:2)

下载ProcDump。运行procdump -t -w app.exe ...或可能procdump -e -w app.exe ...。探索其他旗帜。然后在您喜欢的调试器(Visual Studio / WinDbg)中打开转储文件,并查看堆栈跟踪。

答案 3 :(得分:1)

跟踪/记录功能非常强大,尤其是当客户遇到问题时。您无法始终进行调试,转储可能无法提供导致该点的完整视图。它绝对是对转储或调试的补充。

您也可以打开和关闭它甚至切换关卡。

DebugView是捕获跟踪的好程序:

http://technet.microsoft.com/en-us/sysinternals/bb896647

跟踪:

http://msdn.microsoft.com/en-us/library/3at424ac.aspx

例如,这是一个带有可切换级别的示例跟踪类:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.Threading;

namespace Sample
{
    public class Tracer
    {
        //
        // supports holding many trace switches.
        //
        static Dictionary<string, TraceSwitch> s_switches = new Dictionary<string, TraceSwitch>();
        static TraceSwitch s_switch = new TraceSwitch("trace", "Default tracing switch");
        static object s_locker = new object();

        private static TraceSwitch GetSwitch (string category)
        {
            // only pay the lock penalty if it doesn't exist
            if (!s_switches.ContainsKey (category))
            {
                lock (s_locker)
                {
                    if (!s_switches.ContainsKey (category))
                    {
                        TraceSwitch traceSwitch = new TraceSwitch(category,
                                    String.Format("Tracing switch for category '{0}'", category));
                        s_switches.Add (category, traceSwitch);
                    }
                }
            }

            return s_switches[category];
        }

        //
        // No level overloads
        //
        public static void Output(string message)
        {
            WriteLine("None", TraceLevel.Info, message);
        }

        public static void OutputIf(bool condition, string message)
        {
            if (condition)
            {
                Output(message);
            }
        }

        public static void Output(string format, params object[] args)
        {
            Debug.Assert(format != null);
            Output(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void OutputIf(bool condition, string format, params object[] args)
        {
            if (condition)
            {
                Output(format, args);
            }
        }

        //
        // Error level overloads
        //
        public static void Error(string message)
        {
            if (s_switch.TraceError)
            {
                WriteLine(String.Empty, TraceLevel.Error, message);
            }
        }

        public static void Error(string category, string message)
        {
            if (GetSwitch(category).TraceError)
            {
                WriteLine(category, TraceLevel.Error, message);
            }
        }

        public static void ErrorIf(bool condition, string message)
        {
            if (condition)
            {
                Error(message);
            }
        }

        public static void ErrorIf(string category, bool condition, string message)
        {
            if (condition)
            {
                Error(category, message);
            }
        }

        public static void Error(string format, params object[] args)
        {
            Debug.Assert(format != null);
            Error(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void Error(string category, string format, params object[] args)
        {
            Debug.Assert(format != null);
            Error(category, string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void ErrorIf(bool condition, string format, params object[] args)
        {
            if (condition)
            {
                Error(format, args);
            }
        }

        public static void ErrorIf(string category,
                                   bool condition,
                                   string format,
                                   params object[] args)
        {
            if (condition)
            {
                Error(category, format, args);
            }
        }

        //
        // Warning level overloads
        //
        public static void Warning(string message)
        {
            if (s_switch.TraceWarning)
            {
                WriteLine(String.Empty, TraceLevel.Warning, message);
            }
        }

        public static void Warning(string category, string message)
        {
            if (GetSwitch(category).TraceWarning)
            {
                WriteLine(category, TraceLevel.Warning, message);
            }
        }

        public static void WarningIf(bool condition, string message)
        {
            if (condition)
            {
                Warning(message);
            }
        }

        public static void WarningIf(string category, bool condition, string message)
        {
            if (condition)
            {
                Warning(category, message);
            }
        }

        public static void Warning(string format, params object[] args)
        {
            Debug.Assert(format != null);
            Warning(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void Warning(string category, string format, params object[] args)
        {
            Debug.Assert(format != null);
            Warning(category, string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void WarningIf(bool condition, string format, params object[] args)
        {
            if (condition)
            {
                Warning(format, args);
            }
        }

        public static void WarningIf(string category,
                                     bool condition,
                                     string format,
                                     params object[] args)
        {
            if (condition)
            {
                Warning(category, format, args);
            }
        }

        //
        // Info level overloads
        //
        public static void Info(string message)
        {
            if (s_switch.TraceInfo)
            {
                WriteLine(String.Empty, TraceLevel.Info, message);
            }
        }

        public static void Info(string category, string message)
        {
            if (GetSwitch(category).TraceInfo)
            {
                WriteLine(category, TraceLevel.Info, message);
            }
        }

        public static void InfoIf(bool condition, string message)
        {
            if (condition)
            {
                Info(message);
            }
        }

        public static void InfoIf(string category, bool condition, string message)
        {
            if (condition)
            {
                Info(category, message);
            }
        }

        public static void Info(string format, params object[] args)
        {
            Debug.Assert(format != null);
            Info(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void Info(string category, string format, params object[] args)
        {
            Debug.Assert(format != null);
            Info(category, string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void InfoIf(bool condition, string format, params object[] args)
        {
            if (condition)
            {
                Info(format, args);
            }
        }

        public static void InfoIf(string category,
                                  bool condition,
                                  string format,
                                  params object[] args)
        {
            if (condition)
            {
                Info(category, format, args);
            }
        }

        //
        // Verbose level overloads
        //
        public static void Verbose(string message)
        {
            try
            {
                if (s_switch.TraceVerbose)
                {
                    WriteLine(String.Empty, TraceLevel.Verbose, message);
                }
            }catch{}
        }

        public static void Verbose(string category, string message)
        {
            if (GetSwitch(category).TraceVerbose)
            {
                WriteLine(category, TraceLevel.Verbose, message);
            }
        }

        public static void VerboseIf(bool condition, string message)
        {
            if (condition)
            {
                Verbose(message);
            }
        }

        public static void VerboseIf(string category, bool condition, string message)
        {
            if (condition)
            {
                Verbose(category, message);
            }
        }

        public static void Verbose(string format, params object[] args)
        {
            Debug.Assert(format != null);
            Verbose(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void Verbose(string category, string format, params object[] args)
        {
            Debug.Assert(format != null);
            Verbose(category, string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public static void VerboseIf(bool condition, string format, params object[] args)
        {
            if (condition)
            {
                Verbose(format, args);
            }
        }

        public static void VerboseIf(string category,
                                     bool condition,
                                     string format,
                                     params object[] args)
        {
            if (condition)
            {
                Verbose(category, format, args);
            }
        }

        //
        // Trace Output Format:
        // [category:level]PID|ThreadID|08:16:15.134| message.
        //
        private static void WriteLine(string category,
                                      System.Diagnostics.TraceLevel level,
                                      string message)
        {
            Debug.Assert(message != null);

            string traceLine = string.Format(
                CultureInfo.InvariantCulture,
                "[{0}:{1}]{2}|{3}|{4:HH}:{4:mm}:{4:ss}.{4:fff}|{5}",
                category,
                level.ToString(),
                Process.GetCurrentProcess().Id,
                Thread.CurrentThread.ManagedThreadId,
                DateTime.Now,
                message);

            Trace.WriteLine(traceLine);
        }
    }
}