【异常处理】将错误日志抛向后台以-.txt的保存

一、前言

      前几天的时候,验收机房合作的时候,师哥师姐说可以将错误提交到后台,自己其实以前也知道,但是没有动手实践,验收完后自己经过查资料,完成了这个例子。

二、异常处理

2.1 概念

      异常处理,是编程语言或计算机硬件里的一种机制,用于处理软件或信息系统中出现的异常状况(即超出程序正常执行流程的某些特殊条件)。

2.2 分类

      分类的基类是Throwable,Error是指系统处理不了的错误;Exception是系统可以处理的错误,RuntimeException是经常出的错误。

异常处理分类

                                    图一  异常处理分类

2.3 捕获和处理

错误处理中有五个关键词:try、catch、finally 、throw、throws。

        try {
        //这里放可能会发生异常的语句

        } catch(Exception e) {
        //这里处理异常

        } finally {
        //这里的语句必然会得到执行,不管异常发省与否,
        //用于关闭数据库,关闭连接等收尾操作(非必要)

        }

try、catch、finally 在前面的代码中有介绍。

throw 仅用于方法定义后面,指示该方法可能会抛出什么异常,使用该方法的方法必须处理该异常,或者再次抛出。

throws 用于当程序判断发生异常时,用该语句抛出异常,或处理异常时再次抛出异常。

三、抛向后台Code

抛出错误展示:

抛出错误展示

后台代码:

/****************************************************************************************
 * * 作者:王雷
 * 小组:暂无
 * 说明:【异常处理】将错误日志抛向后台以*.txt的保存
 * 创建日期:20165517:07:58
 * 版本号:V1.0.0
 *****************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GetLog
{
    public partial class testLog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_Click(object sender, EventArgs e)
        {
            try
            {
                int intStr=Convert.ToInt32(tb.Text);
                tb2.Text ="转换成功:" +intStr.ToString();
            }
            catch (Exception ex)
            {
                //错误展出
                WriteLog.WriteError(ex.ToString());
                throw ex;
            }
        }
    }
}

调用抛出日志类:

/****************************************************************************************
 * * 作者:王雷
 * 小组:暂无
 * 说明:【异常处理】将错误日志抛向后台以*.txt的保存
 * 创建日期:20165517:07:58
 * 版本号:V1.0.0
 *****************************************************************************************/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;

namespace GetLog
{
    public class WriteLog
    {
        private static StreamWriter streamWriter; //写文件  

        public static void WriteError(string message)
        {
            try
            {
                //DateTime dt = new DateTime();
                string directPath = ConfigurationManager.AppSettings["LogFilePath"].ToString().Trim();    //获得文件夹路径
                if (!Directory.Exists(directPath))   //判断文件夹是否存在,如果不存在则创建
                {
                    Directory.CreateDirectory(directPath);
                }
                directPath += string.Format(@"\{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));
                if (streamWriter == null)
                {
                    streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath);    //判断文件是否存在如果不存在则创建,如果存在则添加。
                }
                streamWriter.WriteLine("***********************************************************************");
                streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
                streamWriter.WriteLine("输出信息:错误信息");
                if (message != null)
                {
                    streamWriter.WriteLine("异常信息:\r\n" + message);
                }
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Flush();
                    streamWriter.Dispose();
                    streamWriter = null;
                }
            }
        }
    }
}

      想要源码:https://yunpan.cn/cPnceihZjhuR5 访问密码 d86c,请移步。

四、小结

      出来混总是要还的,以前学习过的时候没有总结过这里的,但是还是要总结一下,通过这个的总结,可以更好的完善自己的代码库~~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 69
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 69
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你个佬六

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值