时间:2021-07-01 10:21:17 帮助过:2人阅读
namespace DotNet.Utilities { /// <summary> /// SQLTrace /// 记录SQL执行 Global 中设置 BaseSystemInfo.LogSQL=true 可以开启跟踪记录 /// /// /// 修改纪录 /// /// 2016.01.12 版本:1.0 SongBiao /// /// <author> /// <name>SongBiao</name> /// <date>2016.01.12</date> /// </author> /// </summary> public class SQLTrace { private static string FileName = "SQLTrace.txt"; #region public static void WriteLog(string commandText,IDbDataParameter[] dbParameters = null, string fileName = null) 写入sql查询句日志 /// <summary> /// 写入sql查询句日志 /// </summary> /// <param name="commandText">异常</param> /// <param name="dbParameters"></param> /// <param name="fileName">文件名</param> public static void WriteLog(string commandText, IDbDataParameter[] dbParameters = null, string fileName = null) { // 系统里应该可以配置是否记录异常现象 if (!BaseSystemInfo.LogSQL) { return; } if (string.IsNullOrEmpty(fileName)) { fileName = DateTime.Now.ToString(BaseSystemInfo.DateFormat) + " _ " + FileName; } string message = string.Empty; message = DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + System.Environment.NewLine + "commandText内容" + System.Environment.NewLine + commandText; if (dbParameters != null) { StringBuilder sb = new StringBuilder(); foreach (var parameter in dbParameters) { sb.AppendLine(parameter.ParameterName + "=" + parameter.Value); } message += System.Environment.NewLine + "dbParameters内容" + System.Environment.NewLine + sb.ToString(); } // 将异常信息写入本地文件中 string logDirectory = BaseSystemInfo.StartupPath + @"\Log\Query"; if (!System.IO.Directory.Exists(logDirectory)) { System.IO.Directory.CreateDirectory(logDirectory); } string writerFileName = logDirectory + "\\" + fileName; FileUtil.WriteMessage(message, writerFileName); } #endregion } }
方法比较简单,传入参数化的SQL语句及参数数组,就可以在文件中记录每次具体的操作语句。如下图:(访问的是MySQL数据库)