XuGang

记录一个程序员的成长

 

asp.net记录出错信息log.txt的代码

 

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5namespace JYK.DataAccessLibrary
 6{
 7     public class WriteLog
 8     {
 9         #region 设置出错信息
10         /// <summary>
11         /// 当发生异常时,所作的处理
12         /// </summary>
13         /// <param name="FunctionName">函数名称</param>
14         /// <param name="commandText">查询语句或者存储过程</param>
15         /// <param name="message">错误信息</param>
16        public static string SetErrorMsg(string FunctionName, string commandText, string message)
17         {            //设置返回到上一层的错误信息
19             string errorMsg = FunctionName + "函数出现错误。<BR>错误信息:" + message;
20             if (WebConfig.isShowErrorSQL())
21             {
22                 errorMsg += "<BR>查询语句:" + commandText;
23                 addLogErr(commandText, errorMsg);    //记录到错误日志
24            }
25             else
26             {    //记录到错误日志
27                addLogErr(commandText, errorMsg + "<BR>查询语句:" + commandText);   
28             }
29             return errorMsg;
31         }
32         #endregion
34         #region 记录错误日志
35         //如果要使用的话,根据你的需要进行修改。
36        public static void addLogErr(string SPName, string ErrDescribe)
37         {
38             //记录到错误日志
39            string FilePath = System.Web.HttpContext.Current.Server.MapPath("/log/" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
40             System.Text.StringBuilder str = new System.Text.StringBuilder();
41             str.Append(DateTime.Now.ToString());
42             str.Append(""t");
43             str.Append(SPName);
44             str.Append(""t");
45             str.Append(ErrDescribe.Replace("<BR>", ""));
46             str.Append(""r"n");
47
48             System.IO.StreamWriter sw = null;
49             try
50             {
51                 sw = new System.IO.StreamWriter(FilePath, true, System.Text.Encoding.Unicode);
52                 sw.Write(str.ToString());
53             }
54             catch (Exception ex)
55             {
56                 System.Web.HttpContext.Current.Response.Write("没有访问日志文件的权限!或日志文件只读!<BR>" + ex.Message);
57             }
58             finally
59             {
60                 if (sw != null)
61                     sw.Close();
62             }
63         }
64         #endregion
65     }
66 }
67


这个对于经常在程序里面使用SQL语句的兄弟们是很实用的,因为它会把出错的SQL语句、出错的描述(ex.Message)、函数名称、出错的时间,写到一个文本文件里面。对于查错是很方便的。存储过程的话帮助不是很大,因为存储过程的出错描述总是让人很晕,记录下来帮助也不是很大。


http://www.cnblogs.com/jyk/archive/2007/04/29/sqlhelp.html

读取webconfig里的连接字符串

public static readonly string ConnectionString = System.Web.Configuration.WebConfigurationManager.AppSettings["ConnString"];

posted on   钢钢  阅读(1726)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

统计

点击右上角即可分享
微信分享提示