program.cs

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationException.Run(call); // 调用异常信息捕获类,进行异常信息的捕获
}

// 应用程序,入口逻辑
public static void call()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

LogFile.instance.Start();
string Msg = string.Format("程序开始启动!!!");
LogFile.instance.LogInfo(Msg);
Application.Run(new FormMain());
}
}

ApplicationException.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Degassing
{
/// <summary>
/// 此类用于捕获Application异常信息
/// </summary>
class ApplicationException
{
/// <summary>
/// 定义委托接口处理函数,调用此类中的Main函数为应用添加异常信息捕获
/// </summary>
public delegate void ExceptionCall();

public static void Run(ExceptionCall exCall)
{
try
{
//设置应用程序处理异常方式:ThreadException处理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

if (exCall != null) exCall();
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex, string.Empty);
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}

/// <summary>
/// 生成自定义异常消息
/// </summary>
/// <param name="ex">异常对象</param>
/// <param name="backStr">备用异常消息:当ex为null时有效</param>
/// <returns>异常字符串文本</returns>
static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************异常文本****************************");
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
if (ex != null)
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
sb.AppendLine("【异常方法】:" + ex.TargetSite);
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
sb.AppendLine("***************************************************************");

string strMsg = sb.ToString();
WriteLog(strMsg);
return strMsg;
}

static void WriteLog(string msg)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Application.StartupPath + "/log/exception.txt", true))
{
file.WriteLine(msg);
}
}
}
}

C# 应用异常捕获的更多相关文章

  1. .NET 基础 一步步 一幕幕[数组、集合、异常捕获]

    数组.集合.异常捕获 数组: 一次性存储多个相同类型的变量. 一维数组: 语法: 数组类型[] 数组名=new 数组类型[数组长度]; 声明数组的语法: A.数据类型 [] 数组名称= new 数据类 ...

  2. MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获

    public class BaseController : Controller { /// <summary> /// Called after the action method is ...

  3. atitit.js浏览器环境下的全局异常捕获

    atitit.js浏览器环境下的全局异常捕获 window.onerror = function(errorMessage, scriptURI, lineNumber) { var s= JSON. ...

  4. C#中的那些全局异常捕获

    1.WPF全局捕获异常     public partial class App : Application     {         public App()         {    // 在异 ...

  5. Spring-MVC开发之全局异常捕获全面解读

    异常,异常 我们一定要捕获一切该死的异常,宁可错杀一千也不能放过一个! 产品上线后的异常更要命,一定要屏蔽错误内容,以免暴露敏感信息! 在用Spring MVC开发WEB应用时捕获全局异常的方法基本有 ...

  6. JavaScript异常捕获

    理论准备 ★   异常捕获 △ 异常:当JavaScript引擎执行JavaScript代码时,发生了错误,导致程序停止运行: △ 异常抛出:当异常产生,并且这个异常生成一个错误信息: △ 异常捕获: ...

  7. SQLServer异常捕获

    在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...

  8. Asp.Net MVC3(三)-MvcApp实现全局异常捕获

    定义异常捕获类: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMu ...

  9. iphone 异常捕获处理

    iphone 异常捕获处理 1 void UncaughtExceptionHandler(NSException *exception) { 2 NSArray *arr = [exception ...

  10. iOS异常捕获

    文章目录 一. 系统Crash 二. 处理signal 下面是一些信号说明 关键点注意 三. 实战 四. Crash Callstack分析 – 进⼀一步分析 五. demo地址 六. 参考文献 前言 ...

随机推荐

  1. nyoj--95--众数问题(水题)

    众数问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数, 多重集合S重的重 ...

  2. 什么是EL表达式

    转自:https://blog.csdn.net/hj7jay/article/details/51302466 1. EL表达式主要作用 EL表达式说白了,就是让JSP写起来更加方便,它属于JSP技 ...

  3. ubuntu DNS 出错,用以下命令可以解决

    具体的错误为:DNS_PROBE_FINISHED_BAD_CONFIG 命令为: sudo rm /etc/resolv.conf sudo ln -s ../run/resolvconf/reso ...

  4. vue-cli安装步骤

    vue-cli脚手架模板是基于node下的npm来完成安装的所以首先需要安装node 条件:  node在4.以上,npm在3以上 安装 指令: 1.npm install -g vue-cli 在全 ...

  5. Windows下绿色版Tomcat部署Thingworx 7.4

    绿色版Tomcat部署Thingworx7.4和安装只有一个不同之处,安装版Tomcat需要在Configure Tomcat的Java标签下设置Java Options,但是绿色版并没有这个exe程 ...

  6. MySQL的几个重要配置参数详解

    .配置通用查询日志,需要在配置文件my.cnf中增加如下: () 在mysql的安装目录下,修改my.cnf配置文件,增加general_log = () 重启mysql,可执行命令/etc/init ...

  7. @DateTimeFormat无效原因

    一般都是使用@DateTimeFormat把传给后台的时间字符串转成Date,使用@JsonFormat把后台传出的Date转成时间字符串,但是@DateTimeFormat只会在类似@Request ...

  8. python 多列表对应的位置的值形成一个新的列表

    list1 = [1, 2, 3, 4, 5] list2 = ['a','b', 'c', 'd', 'e'] list3 = [1, 2, 3, 4, 5] multi_list = map(li ...

  9. Django Rest Framework 简介及 初步使用

    使用Django Rest Framework之前我们要先知道,它是什么,能干什么用? Django Rest Framework 是一个强大且灵活的工具包,用以构建Web API 为什么要使用Res ...

  10. jQuery 简单介绍

    jQuery  简单介绍 jQuery的定义 jQuery是一个快速,小巧,功能丰富的JavaScript库.它通过易于使用的API在大量浏览器中运行,使得   HTML文档遍历和操作,事件处理,动画 ...