c# winform捕获全局异常,并记录日志
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO; namespace OrderSplit
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
try
{
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += Application_ThreadException;
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1());
}
catch (Exception ex)
{
var strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now + "\r\n"; var str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
ex.GetType().Name, ex.Message, ex.StackTrace); WriteLog(str);
MessageBox.Show("发生错误,请查看程序日志!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit();
}
} /// <summary>
///错误弹窗
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str;
var strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now + "\r\n";
var error = e.Exception;
if (error != null)
{
str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
error.GetType().Name, error.Message, error.StackTrace);
}
else
{
str = string.Format("应用程序线程错误:{0}", e);
} WriteLog(str);
MessageBox.Show("发生错误,请查看程序日志!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit();
} static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var error = e.ExceptionObject as Exception;
var strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now + "\r\n";
var str = error != null ? string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace) : string.Format("Application UnhandledError:{0}", e); WriteLog(str);
MessageBox.Show("发生错误,请查看程序日志!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit();
}
/// <summary>
/// 写文件
/// </summary>
/// <param name="str"></param>
static void WriteLog(string str)
{
if (!Directory.Exists("ErrLog"))
{
Directory.CreateDirectory("ErrLog");
} using (var sw = new StreamWriter(@"ErrLog\ErrLog.txt", true))
{
sw.WriteLine(str);
sw.WriteLine("---------------------------------------------------------");
sw.Close();
}
}
}
}
c# winform捕获全局异常,并记录日志的更多相关文章
- C# WinForm捕获全局异常
网上找的C# WinForm全局异常捕获方法,代码如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summ ...
- C# WINFORM 捕获全局异常
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Thr ...
- C# WinForm捕获全局异常(捕获未处理的异常)
static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
毕竟人不是神,谁写的程序都会有bug,有了bug不可怕,可怕的是出错了,你却不知道错误在哪里.所以我们需要将应用程序中抛出的所有异常都记录起来,不然出了错,找问题就能要了你的命.下面我们主要讨论的是如 ...
- winform 记录全局异常捕获
这篇文章主要是备用 记录winform程序捕获全局异常. /// <summary> /// 应用程序的主入口点. /// </summary> public static A ...
- 如何捕获winform程序全局异常?(续)
前言 上篇文章我提供了一种方案可以供我们捕获单线程程序中的所有未处理异常.但是如果程序是多线程,那么新增线程出现了异常上个方案就无能为力了.本着方案总比问题多的态度,我再给大家提供一种新的方案,供大家 ...
- WebForm 在 Global.asax 中捕获全局异常
/// <summary> /// 捕获全局异常 /// </summary> /// <param name="sender">sender& ...
- C# 捕获全局异常
一.在Winform程序中捕获全局异常 在winfrom中我们需要了解Application对象中的两个事件 ①Application.ThreadException 事件--当UI线程中某个异常未被 ...
- Android捕获全局异常
Android捕获全局异常 程序避免不了出现bug,导致程序崩溃,为了尽量不影响用户体验,可以全局捕获异常 效果图 异常捕获处理前 异常捕获处理后(将程序重新启动) 捕获异常的工具类 package ...
随机推荐
- 二叉树的层次遍历(Java代码实现)
与树的前中后序遍历的DFS思想不同,层次遍历用到的是BFS思想.一般DFS用递归去实现(也可以用栈实现),BFS需要用队列去实现. 层次遍历的步骤是: 1.对于不为空的结点,先把该结点加入到队列中 2 ...
- Response attachment filename 中文乱码
Response.setHeader("Content-Disposition", "attachment; filename=" + fileName+&qu ...
- C#中【正则表达式regular expression】相关的知识
Regex System.Text.RegularExpressions.Regex regex应该是regular expression的缩写 https://msdn.microsoft ...
- Spring Container的扩展点
转自: http://blog.csdn.net/kkdelta/article/details/5488430 Spring在解析完配置文件后,会调用一些callback方法,使用Spring的开发 ...
- input[type="file"]的样式以及文件名的显示
如何美化input[type="file"] 基本思路是: (1)首先在 input 外层套一个 div : (2)将 div 和 input 设置为一样大小(width和heig ...
- 复习指南(Pascal版)
[第一层级 条件反射] 1.个十百千各数位的求法 q:=a div 1000 mod 10; b:=a div 100 mod 10; s:=a div 10 mod 10; g:=a mod 10; ...
- spring 或 springboot统一异常处理
spring 或 springboot统一异常处理https://blog.csdn.net/xzmeasy/article/details/76150370 一,本文介绍spring MVC的自定义 ...
- ubuntu 18. use gnome-tweaks
<<install gnome-tweaks sudo apt-get install gnome-tweaks <<run gnome-tweaks >>pres ...
- Memcached get 命令
Memcached get 命令获取存储在 key(键) 中的 value(数据值) ,如果 key 不存在,则返回空. 语法: get 命令的基本语法格式如下: get key 多个 key 使用空 ...
- Tomcat 的context.xml说明、Context标签讲解
Tomcat的context.xml说明.Context标签讲解 1. 在tomcat 5.5之前 --------------------------- Context体现在/conf/server ...