当前位置:首页 > C# > 正文内容

c#程序闪退日志记录/异常日志

admin3年前 (2021-09-29)C#3345
以下代码是程序入口文件
using DDS_Form1;
using System;
using System.Collections.Generic;
using System.IO;
//using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    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(0);
            }
        }
        /// <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(0);
        }

        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(0);
        }
        /// <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();
            }
        }
    }
}

扫描二维码推送至手机访问。

版权声明:本文由视觉博客发布,如需转载请注明出处。

本文链接:http://www.cqroom.cn/post/125.html

“c#程序闪退日志记录/异常日志” 的相关文章

安装包制作工具 SetupFactory使用1 详解

安装包制作工具 SetupFactory使用1 详解

Setup Factory 是一个强大的安装程序制作工具。提供了安装制作向导界面,即使你对安装制作不了解,也可以生成专业性质的安装程序。可建立快捷方式,也可直接在 Windows 系统的注册表加入内容,还能在 Win.ini 和 System.ini 内加入设定值,更可以建立反安装选...

C#修改图片分辨率

public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH) { try { Bitmap b =...

C#实现邮件发送的功能

很多时候需要邮件发送功能,例如:监测应用,需要上报状态。   微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)   微软封装好的SmtpClient类:主要处理用smt...

C#全局监听Windows键盘事件

1.工具类代码 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows...

C# 窗体间传值(Form与From之间互相传值)

C# 窗体间传值(Form与From之间互相传值)

1、委托   两个窗体,窗体很简单,只实现改变颜色功能,一看就会: 代码如下,只贴按钮事件代码: 打开Form2按钮事件 private void button1_Click(object s...

高恪路由器批量管理监控系统

高恪路由器批量管理监控系统

一、采用C# 编写,可以批量监控路由器是否正常 功能: 1.显示当前路由IP、名字、实时上行速度、实时下行速度、主机数量、连接数、CPU占用率、内存占用率、CPU温度、运行时间等 2.可以显示10分钟实时流量 3.可以显示历史2小时、...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。