在开发时期记录程序异常(并将其保存在文本中)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DataLibrary.Log
{
    
/// <summary>
    
/// 数据库日志参数类
    
/// </summary>
    public class DataBaseLogArgs
    {
        
/// <summary>
        
/// 异常详细信息
        
/// </summary>
        public string ExceptionDetails;
        
/// <summary>
        
/// 数据库变量
        
/// </summary>
        public SqlParameter[] SqlParameter;
        
/// <summary>
        
/// 命令字符串
        
/// </summary>
        public string CommandText;
        
/// <summary>
        
/// 执行类型
        
/// </summary>
        public CommandType CommandType;
        
/// <summary>
        
/// 执行的整知语句及方法
        
/// </summary>
        public string Command;
        
/// <summary>
        
/// 时间
        
/// </summary>
        public DateTime DateTime;
        
/// <summary>
        
/// 日志类型
        
/// </summary>
        public LogType LogType;
    }
    
/// <summary>
    
/// 日志类型
    
/// </summary>
    public enum LogType
    {
        
/// <summary>
        
/// 错误
        
/// </summary>
        Error,
        
/// <summary>
        
/// 警告
        
/// </summary>
        Warning,
        
/// <summary>
        
/// 日志记录
        
/// </summary>
        Log
    }
}
//用法 只在调试的时候应用
//#if DEBUG
//            try
//            {
//#endif
//                if(tm!=null)
//                {
//                    returnValue = SqlHelper.ExecuteNonQuery(tm.SqlTransaction, this.SqlStruct.CommandType, this.SqlStruct.CommandText, this.SqlStruct.SqlParameters);
//                }
//                else
//                {
//                    returnValue = SqlHelper.ExecuteNonQuery(this.connectStringReadAndWrite, this.SqlStruct.CommandType, this.SqlStruct.CommandText, this.SqlStruct.SqlParameters);
//                }
//#if DEBUG
//            }
//            catch (Exception err)
//            {
//                DataBaseLogArgs args = new DataBaseLogArgs();
//                args.ExceptionDetails = err.ToString();
//                    args.CommandText = this.SqlStruct.CommandText;
//                    args.CommandType = this.SqlStruct.CommandType;
//                args.DateTime = DateTime.Now;
//                args.Command = "ExecuteNonQuery";
//                args.LogType = LogType.Error;
//                args.SqlParameter = this.SqlStruct.SqlParameters;
//                Log.Log.WriteLog(args);
//                throw;
//            }
//#endif   
//            OnUpdate(returnValue);
            
//            return returnValue;
//        }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;

namespace DataLibrary.Log
{
    
/// <summary>
    
/// 数据库操作类日志操作类
    
/// </summary>
    public class Log
    {
        
/// <summary>
        
/// 日志存放根路径 
        
/// </summary>
        static string rootPath = System.Web.HttpRuntime.AppDomainAppPath;

        
/// <summary>
        
/// 写入异常信息到文件
        
/// </summary>
        
/// <param name="args"></param>
        public static void  WriteLog(DataBaseLogArgs args)
        {
            StringBuilder sb 
= new StringBuilder();
            sb.AppendFormat(
"----------------------日志记录于{0},类型为:{1}------------------------{2}", args.DateTime, args.LogType, Environment.NewLine);
            sb.AppendFormat(
">>>>>>>>Sql语句为:{0}{1}", args.CommandText, Environment.NewLine);
            sb.AppendFormat(
">>>>>>>>CommandType为:{0}{1}", args.CommandType, Environment.NewLine);
            sb.AppendFormat(
">>>>>>>>执行命令字符串为:{0}{1}", args.Command, Environment.NewLine);
            
if (args.SqlParameter != null)
            {
                
foreach (SqlParameter sp in args.SqlParameter)
                {
                    sb.AppendFormat(
">>>>>>>>数据库变量:{0}={1}{2}", sp.ParameterName, sp.Value.ToString(), Environment.NewLine);
                }
            }
            
else
            {
                sb.AppendFormat(
">>>>>>>>数据库变量:{0}{1}""", Environment.NewLine);
            }
            sb.AppendFormat(
">>>>>>>>发生于:{0}{1}", args.DateTime, Environment.NewLine);
            sb.AppendFormat(
">>>>>>>>异常详细信息:{0}{1}", args.ExceptionDetails, Environment.NewLine);
            CreateFile(sb.ToString(), rootPath 
+ "\\Log.txt");
        }

        
/// <summary>
        
/// 保存文件
        
/// </summary>
        
/// <param name="code">写入的内容字符串</param>
        
/// <param name="filePath">写入的路径</param>
        
/// <returns></returns>
        protected static bool CreateFile(string code, string filePath)
        {
            
string path = "";
            path 
= Path.GetDirectoryName(filePath);
            
if ((path.Length > 0&& !Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            
using (StreamWriter writer = new StreamWriter(filePath,true, Encoding.GetEncoding("GB2312")))
            {
                writer.Write(code);
                writer.Close();
                writer.Dispose();
                
return true;
            }
        }

    }
}
posted @ 2009-08-06 14:25  awp110  阅读(243)  评论(0编辑  收藏  举报