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 ...
随机推荐
- 一道C++练习题,替换一个字符串里所有实例
做了一道C++练习题,替换一个字符串里面的所有实例. #include <iostream> #include <string> using namespace std; co ...
- windows上面非常好用的辅助软件
1.everything 快速查找本地文件 下载地址:http://www.voidtools.com/
- 广播机制的CS模型实现
广播机制的cs模型实现如下: 首先可以使用ifconfig命令查看自己所在网段的广播地址 server.c #include<stdio.h> #include<unistd.h&g ...
- intellij 文件太大,无法code assistant
添加 idea.max.intellisense.filesize=2500 在IDE_HOME\bin\idea.properties https://intellij-support.jetbra ...
- nuget sources
https://docs.microsoft.com/en-us/nuget/tools/cli-ref-sources https://gemfury.com/help/nuget-server/ ...
- Mac Homebrew安装php56 到phpstorm过程问题汇总
Mac自带版本是php5.5,本来是用homebrew安装xdebug 命令:brew install php55-xdebug 但是安装之后使用phpstorm还是有问题.php -v 并没有显示有 ...
- sql server 跨数据库调用存储过程
A库存储过程: create PROCEDURE [dbo].[spAAAForTest] ( ) =null , ) =null ) AS BEGIN select N'A' AS a , N'B' ...
- 用cookies判断用户首次登录
要求:判断24小时内用户是否是首次登录,如果是则显示弹窗,如果不是则不再显示弹窗 (function() { //是否是新访客 function isNewVisitor() { //从cookie读 ...
- linux加域退域
realm list realm leave od.com realm join -U Administrator od.com
- linux bash shell中,单引号、 双引号,反引号(``)的区别及各种括号的区别
一.单引号和双引号 首先,单引号和双引号,都是为了解决中间有空格的问题. 因为空格在Linux中时作为一个很典型的分隔符,比如string1=this is astring,这样执行就会报错.为了避免 ...