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

推荐订阅源

S
SegmentFault 最新的问题
Spread Privacy
Spread Privacy
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
SecWiki News
SecWiki News
腾讯CDC
P
Privacy International News Feed
Webroot Blog
Webroot Blog
J
Java Code Geeks
爱范儿
爱范儿
A
About on SuperTechFans
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
Cloudbric
Cloudbric
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Security Latest
Security Latest
Forbes - Security
Forbes - Security
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threatpost
量子位
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
V
Visual Studio Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 聂微东
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs

博客园 - 衣明志

给自己用的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);