111 lines
3.7 KiB
C#
111 lines
3.7 KiB
C#
using System;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DataAccessLayer
|
|
{
|
|
public static class SqlLogging
|
|
{
|
|
private static readonly string LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SqlLogs");
|
|
private static readonly object lockObj = new object();
|
|
|
|
static SqlLogging()
|
|
{
|
|
if (!Directory.Exists(LogPath))
|
|
{
|
|
Directory.CreateDirectory(LogPath);
|
|
}
|
|
}
|
|
|
|
private static string GetLogFileName()
|
|
{
|
|
return Path.Combine(LogPath, $"sql_log_{DateTime.Now:yyyyMMdd}.txt");
|
|
}
|
|
|
|
public static void LogStoredProcedure(string spName, Dictionary<string, object> parameters = null, long executionTimeMs = 0)
|
|
{
|
|
try
|
|
{
|
|
var logBuilder = new StringBuilder();
|
|
logBuilder.AppendLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] STORED PROCEDURE");
|
|
logBuilder.AppendLine($"Name: {spName}");
|
|
|
|
if (parameters != null && parameters.Count > 0)
|
|
{
|
|
logBuilder.AppendLine("Parameters:");
|
|
foreach (var param in parameters)
|
|
{
|
|
var value = param.Value == DBNull.Value ? "NULL" : param.Value?.ToString() ?? "NULL";
|
|
logBuilder.AppendLine($"\t@{param.Key}: {value}");
|
|
}
|
|
}
|
|
|
|
if (executionTimeMs > 0)
|
|
{
|
|
logBuilder.AppendLine($"Execution Time: {executionTimeMs}ms");
|
|
}
|
|
|
|
logBuilder.AppendLine(new string('-', 80));
|
|
|
|
lock (lockObj)
|
|
{
|
|
File.AppendAllText(GetLogFileName(), logBuilder.ToString());
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error logging stored procedure: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public static void LogCommand(SqlCommand command, long executionTimeMs = 0)
|
|
{
|
|
try
|
|
{
|
|
var logBuilder = new StringBuilder();
|
|
logBuilder.AppendLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}]");
|
|
logBuilder.AppendLine($"Command Type: {command.CommandType}");
|
|
|
|
if (command.CommandType == System.Data.CommandType.StoredProcedure)
|
|
{
|
|
logBuilder.AppendLine($"Stored Procedure Name: {command.CommandText}");
|
|
}
|
|
else
|
|
{
|
|
logBuilder.AppendLine($"Command Text: {command.CommandText}");
|
|
}
|
|
|
|
if (command.Parameters.Count > 0)
|
|
{
|
|
logBuilder.AppendLine("Parameters:");
|
|
foreach (SqlParameter param in command.Parameters)
|
|
{
|
|
var value = param.Value == DBNull.Value ? "NULL" : param.Value?.ToString() ?? "NULL";
|
|
logBuilder.AppendLine($"\t{param.ParameterName} ({param.SqlDbType}): {value}");
|
|
}
|
|
}
|
|
|
|
if (executionTimeMs > 0)
|
|
{
|
|
logBuilder.AppendLine($"Execution Time: {executionTimeMs}ms");
|
|
}
|
|
|
|
logBuilder.AppendLine(new string('-', 80));
|
|
|
|
lock (lockObj)
|
|
{
|
|
File.AppendAllText(GetLogFileName(), logBuilder.ToString());
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Error logging SQL command: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|