客户端软件成品 该如何查看错误/异常(弹出/生成日志)
2018-01-09 14:50
579 查看
1.把异常直接弹出
在代码里找到 App.xaml,下面有个 App.xaml.cs
在 App 类里加这段:
public App()
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString(), "发生异常", MessageBoxButton.OK, MessageBoxImage.Error);
// 由默认处理机制继续处理
e.Handled = false;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if(e.ExceptionObject is Exception ex)
{
MessageBox.Show(ex.ToString(), "发生异常", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
MessageBox.Show("发生异常", "发生异常", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
2. 在exe所在目录生成日志
自己写个生成错误日志的方法
/// <summary>
/// 将异常打印到LOG文件
/// </summary>
/// <param name="ex">异常</param>
/// <param name="LogAddress">日志文件地址</param>
public static void WriteLog(Exception ex, string LogAddress = "")
{
//如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
if (LogAddress == "")
{
LogAddress = Environment.CurrentDirectory + '\\' +
DateTime.Now.Year + '-' +
DateTime.Now.Month + '-' +
DateTime.Now.Day + "_Log.log";
}
//把异常信息输出到文件,因为异常文件由这几部分组成,这样就不用我们自己复制到文档中了
StreamWriter fs = new StreamWriter(LogAddress, true);
//fs.WriteLine("当前时间:" + DateTime.Now.ToString());
//fs.WriteLine("异常信息:" + ex.Message);
//fs.WriteLine("异常对象:" + ex.Source);
//fs.WriteLine("调用堆栈:\n" + ex.StackTrace.Trim());
//fs.WriteLine("触发方法:" + ex.TargetSite);
fs.WriteLine("-----------");
fs.WriteLine(ex.ToString());
fs.WriteLine();
fs.Close();
}
调用上面的方法
try
{
int i = 0;
int j = 2 / i;//0不能作为被除数
}
catch (Exception ex)
{
WriteLog(ex);
}
在代码里找到 App.xaml,下面有个 App.xaml.cs
在 App 类里加这段:
public App()
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString(), "发生异常", MessageBoxButton.OK, MessageBoxImage.Error);
// 由默认处理机制继续处理
e.Handled = false;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if(e.ExceptionObject is Exception ex)
{
MessageBox.Show(ex.ToString(), "发生异常", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
MessageBox.Show("发生异常", "发生异常", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
2. 在exe所在目录生成日志
自己写个生成错误日志的方法
/// <summary>
/// 将异常打印到LOG文件
/// </summary>
/// <param name="ex">异常</param>
/// <param name="LogAddress">日志文件地址</param>
public static void WriteLog(Exception ex, string LogAddress = "")
{
//如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
if (LogAddress == "")
{
LogAddress = Environment.CurrentDirectory + '\\' +
DateTime.Now.Year + '-' +
DateTime.Now.Month + '-' +
DateTime.Now.Day + "_Log.log";
}
//把异常信息输出到文件,因为异常文件由这几部分组成,这样就不用我们自己复制到文档中了
StreamWriter fs = new StreamWriter(LogAddress, true);
//fs.WriteLine("当前时间:" + DateTime.Now.ToString());
//fs.WriteLine("异常信息:" + ex.Message);
//fs.WriteLine("异常对象:" + ex.Source);
//fs.WriteLine("调用堆栈:\n" + ex.StackTrace.Trim());
//fs.WriteLine("触发方法:" + ex.TargetSite);
fs.WriteLine("-----------");
fs.WriteLine(ex.ToString());
fs.WriteLine();
fs.Close();
}
调用上面的方法
try
{
int i = 0;
int j = 2 / i;//0不能作为被除数
}
catch (Exception ex)
{
WriteLog(ex);
}
相关文章推荐
- 由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 <serviceDebug> 配置行为)以便将异常信息发送回客户端,或打开对每个 Microsoft .NET Framework SDK 文档的跟踪并检查服务器跟踪日志。
- 如何区分软件错误和异常
- Linux上如何查看Deb和RPM软件包的更新日志
- SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志(转)
- Linux上如何查看Deb和RPM软件包的更新日志
- 用服务器日志监控软件、服务器日志分析工具软件教你如何查看服务器日志?
- iis错误日志对应的站点如何查看
- 【手把手教你Elmah】如何在MVC.NET项目中在线查看【错误日志】
- SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志
- 软件发布了如何跟踪异常错误
- 如何查看ldiskfs文件系统的错误日志
- 如何通过邮件头和传输跟踪日志查看原始客户端IP 推荐
- SharePoint online 中如何查看搜索的爬网错误日志
- 黄聪:VS2010中如何让webbrowser不弹出JS异常错误窗口(c#.net)
- windows server 2008 如何查看异常重启日志
- SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志(转)
- 如果遇到Hadoop集群正常,MapReduce作业运行出现错误,如何来查看作业运行日志(图文详解)
- linux中如何查看日志中某个异常出现的次数
- SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志(转)
- 学习笔记6-Android查看应用输出的错误信息 如何部署应用到真实手机 发布软件