惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
博客园 - 【当耐特】
H
Heimdal Security Blog
腾讯CDC
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threatpost
月光博客
月光博客
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
D
DataBreaches.Net
O
OpenAI News
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
小众软件
小众软件
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
美团技术团队
P
Privacy International News Feed

博客园 - 衣明志

给自己用的dasblog换皮肤了 2008微软新技术激情碰撞——烟台.Net俱乐部07年末特别活动 SQL2005数据库镜像之数据库用户问题 推荐:《Programming ASP.NET中文版》 Microsoft Anti-Cross Site Scripting Library V1.5 发布了 烟台.Net俱乐部网站近几日出现的非法访问情况 烟台.Net俱乐部11月活动邀请 在Dasblog中添加了个添加表情功能 烟台.Net俱乐部4月活动邀请 3月12日烟台.Net俱乐部活动通知 DasBlog.1.8.5223.2中文版 烟台.NET俱乐部正式成为INETA成员 VS2005简体中文安装包 dasBlog-1.8.5223.1中文版 测试一下DasBlog的异站同步功能 烟台.Net俱乐部10月活动之烟台场 烟台.Net俱乐部10月活动Asp.Net实战之旅 威海行 8.28活动的演讲稿和相关Demo的下载 8.28烟台.Net俱乐部首次活动总结
一个简单的asp.net异常记录程序
衣明志 · 2005-08-26 · via 博客园 - 衣明志

在实际开发Asp.Net项目时,程序可能会出现一些异常,那么我们应该怎么去获得尽量多的系统异常,以便后期维护过程能够尽可能重现异常,及时分析、发现和修复Bug。为了处理这个问题,我写了一个小程序来帮助实现我的需求。

首先,我写了一个日志记录的类,以便能够把捕获的异常以及相关的用户信息以XML方式写入指定目录:

 1using System;
 2using System.Data;
 3using System.Data.SqlClient;
 4using System.IO;
 5using System.Xml;
 6
 7namespace Logs
 8{
 9    /// <summary>
10    /// Logs 的摘要说明。
11    /// </summary>

12    public class WebLogs
13    {
14        private string logPath = string.Empty;
15
16        public WebLogs(string LogPath)
17        {
18            //
19            // TODO: 在此处添加构造函数逻辑
20            //
21            logPath = LogPath;
22        }

23
24        public string FloderPath
25        {
26            get
27            {
28                return logPath;
29            }

30            set
31            {
32                logPath = value;
33            }

34        }

35
36        public void Add(string LogType,string UserName,string Content,string IP,string Agent,string Url,string UrlReferrer)
37        {
38            string filename = LogType+"-"+DateTime.Now.ToShortDateString()+".xml";
39            string filepath = logPath + filename;
40            LogWrite(filepath,UserName,Content,IP,Agent,Url,UrlReferrer);
41        }

42
43        private void LogWrite(string filepath,string UserName,string Content,string IP,string Agent,string Url,string UrlReferrer)
44        {
45            XmlDocument xdoc = new XmlDocument();
46            if(!File.Exists(filepath))
47            {
48                xdoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
49                             "<WebLogs></WebLogs>");
50            }

51            else
52            {
53                xdoc.Load(filepath);
54            }

55
56// 创建一个新的日志节点并将它添加到根节点下
57            XmlElement parentNode = xdoc.CreateElement("WebLog");
58            xdoc.DocumentElement.PrependChild(parentNode);
59            // 创建所有用于存储信息的节点
60            XmlElement nameNode = xdoc.CreateElement("UserName");
61            XmlElement contentNode = xdoc.CreateElement("Content");
62            XmlElement timeNode = xdoc.CreateElement("Time");
63            XmlElement ipNode = xdoc.CreateElement("IP");
64            XmlElement agentNode = xdoc.CreateElement("Agent");
65            XmlElement urlNode = xdoc.CreateElement("Url");
66            XmlElement referrerNode = xdoc.CreateElement("Referrer");
67            // 获取文本信息
68            XmlText nameText = xdoc.CreateTextNode(UserName);
69            XmlText contentText = xdoc.CreateTextNode(Content);
70            XmlText timeText = xdoc.CreateTextNode(DateTime.Now.ToString());
71            XmlText ipText = xdoc.CreateTextNode(IP);
72            XmlText agentText = xdoc.CreateTextNode(Agent);
73            XmlText urlText = xdoc.CreateTextNode(Url);
74            XmlText referrerText = xdoc.CreateTextNode(UrlReferrer);
75// 将上面创建的各个存储信息的节点添加到guest节点下但并不包含最终的值
76            parentNode.AppendChild(nameNode);
77            parentNode.AppendChild(contentNode);
78            parentNode.AppendChild(timeNode);
79            parentNode.AppendChild(ipNode);
80            parentNode.AppendChild(agentNode);
81            parentNode.AppendChild(urlNode);
82            parentNode.AppendChild(referrerNode);
83            // 将上面获取的文本信息添加到与之相对应的节点中
84            nameNode.AppendChild(nameText);
85            contentNode.AppendChild(contentText);
86            timeNode.AppendChild(timeText);
87            ipNode.AppendChild(ipText);
88            agentNode.AppendChild(agentText);
89            urlNode.AppendChild(urlText);
90            referrerNode.AppendChild(referrerText);
91
92            // 保存存储信息的XML文件
93            xdoc.Save(filepath);
94            xdoc = null;
95        }

96    }

97}

然后,我在我的Asp.Net站点的Global.asax.cs的Application_Error事件里加入以下代码:

WebLogs Logs = new WebLogs(Server.MapPath("logs/"));
string referrerUri = (Request.UrlReferrer != null?Request.UrlReferrer.AbsoluteUri : string.Empty;
string UserName = (User.Identity.Name != string.Empty) ? User.Identity.Name : "Anonymous";
Logs.Add(
"Error",UserName,Server.GetLastError().ToString(),Request.UserHostAddress,Request.UserAgent,Request.Url.AbsoluteUri,referrerUri);