您的位置:首页 > 其它

将错误日志抛向后台以-.txt文件保存

2017-06-30 09:11 316 查看

一、前言

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

二、异常处理

2.1 概念

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

2.2 分类

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



图一  异常处理分类


2.3 捕获和处理

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

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

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

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

}
1
2
3
4
5
6
7
8
9
10
11


1
2
3
4
5
6
7
8
9
10
11
[/code]

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

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

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

三、抛向后台Code

抛出错误展示:



后台代码:

/****************************************************************************************
* * 作者:王雷
* 小组:暂无
* 说明:【异常处理】将错误日志抛向后台以*.txt的保存
* 创建日期:2016年5月5日17: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;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[/code]

调用抛出日志类:

/****************************************************************************************
* * 作者:王雷
* 小组:暂无
* 说明:【异常处理】将错误日志抛向后台以*.txt的保存
* 创建日期:2016年5月5日17: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;
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[/code]

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

四、小结

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

【转载地址:http://blog.csdn.net/kisscatforever/article/details/51350510
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐