我不認爲你需要一個app.xaml來註冊這些事件。
如果你想做到這一點,在你的類的一個靜態初始化,將肯定被任何人使用控制庫使用添加以下代碼:
if (AppDomain.CurrentDomain != null) {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
if (Dispatcher.CurrentDispatcher != null) {
Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
// Do something with the exception in e.ExceptionObject
}
static void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
// Do something with the exception in e.Exception
}
我同意你的看法,是不是好的殺死庫中的異常。但我不想殺死例外。我對處理它們感興趣,可以處理一個異常,甚至讓它繼續向上拋出。還有一點是,我沒有分發我的圖書館,我用它來達到一個非常特定的目的。這個庫將成爲系統的一部分,但它不在.net中的系統。所以我需要將例外的日誌放入我的庫中。 –