跟着《WPF专业编程开发指南》这书打的代码的,自己在正式项目中测试通过,可以抓取到全局的异常,用的log4net来记录日志
核心代码:
写在App.xaml.cs中
<pre class="prettyprint lang-cs" style="margin: 10px 0px; padding: 10px; border: 1px solid rgb(204, 204, 204); outline: 0px; font-weight: inherit; font-style: inherit; font-family: "Source Code Pro", Consolas, Monaco, Menlo, Consolas, monospace; font-size: 10px; vertical-align: baseline; position: relative; z-index: 0; line-height: 22.4px; overflow-wrap: break-word; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow: auto; color: rgb(255, 255, 255); text-shadow: rgb(68, 68, 68) 0px 1px; background: rgb(39, 40, 34);"> /// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
}
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
Common.Log.Error($"抓到未知异常:",e.Exception);
}
}</pre>
image
网友评论