zoukankan      html  css  js  c++  java
  • 在开发时期记录程序异常(并将其保存在文本中)

    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;
                }
            }

        }
    }
  • 相关阅读:
    DOS批处理高级教程(三) : 批处理变量和set命令详解
    DOS批处理高级教程(二) DOS循环: 语句命令FOR、IF
    DOS批处理高级教程(一) 批处理基础
    win7下部署个人网站教程
    Ubuntu安装后常见部署
    python3 生成钻石展位后台报表记录
    The Zen of Python
    4刀最多切割一个正方体为多少部分
    Python基础讲义第二弹面向对象编程(淘宝平台模拟为例)
    python基础讲义第一弹
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1540372.html
Copyright © 2011-2022 走看看