グローバル エラー処理の問題。
例外をスローした行を含む詳細を取得したい。
エラーをスローするクラスが MainWindow の ctor で作成された場合、例外のクラス名と行番号が報告されます。
しかし、例外をスローするクラスがイベントハンドラーで作成された場合、詳細はゼロです-例外をスローしたクラスの名前さえ報告しません。イベント ハンドラーによって初期化されたオブジェクトの例外から詳細を取得するにはどうすればよいですか?
namespace GlobalErrorHandler
{
public partial class App : Application
{
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("App_DispatcherUnhandledException Error." + e.Exception.Message + " " + e.Exception.InnerException, "Error");
e.Handled = true;
//if (MainWindow != null) MainWindow.Close();
}
public App()
{
this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
}
}
}
<Window x:Class="GlobalErrorHandler.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="300">
<Grid>
<Button Content="Class1 from Main" Click="Button_Click_Class"
Height="20" Width="100" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</Window>
namespace GlobalErrorHandler
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Class1 MyClass1 = new Class1(); // this gives line detail
//throw new Exception(); // this gives line detail
}
private void Button_Click_Class(object sender, RoutedEventArgs e)
{
Class1 MyClass1 = new Class1(); // this does NOT give line detail
}
}
}
namespace GlobalErrorHandler
{
class Class1
{
public Class1()
{
throw new Exception();
}
}
}
このサンプルはクラスとボタン イベントです。
しかし、ページやその他のイベントでも同じ問題があります。
例外をスローするクラスが Window Loaded イベントで作成されたとしても、詳細はありません。
e.Exception.GetBaseException() を調べましたが、まだ情報がありません。
イライラするのは、例外がスローされるデバッグ モードです。Visual Studio で完全なスタック トレースを表示できますが、App_DispatcherUnhandledException に到達するまでにそのスタック トレースはなくなります。
PDB ファイルを含めてみましたが、修正されませんでした。