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)、函数名称、出错的时间,写到一个文本文件里面。对于查错是很方便的。存储过程的话帮助不是很大,因为存储过程的出错描述总是让人很晕,记录下来帮助也不是很大。



读取webconfig里的连接字符串


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