2025-06-05 15:12:26 +02:00

88 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using NLog;
using System.Text;
namespace DataAccessLayer
{
public static class SqlLogging
{
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
public static void LogSqlStart(string sql, Dictionary<string, object> parameters = null)
{
var sb = new StringBuilder();
sb.AppendLine($"Executing SQL: {sql}");
if (parameters != null && parameters.Any())
{
sb.AppendLine("Parameters:");
foreach (var param in parameters)
{
sb.AppendLine($" @{param.Key} = {param.Value ?? "null"}");
}
}
Logger.Debug(sb.ToString());
}
public static void LogSqlError(string sql, Exception ex, Dictionary<string, object> parameters = null)
{
var sb = new StringBuilder();
sb.AppendLine($"SQL Error in query: {sql}");
if (parameters != null && parameters.Any())
{
sb.AppendLine("Parameters:");
foreach (var param in parameters)
{
sb.AppendLine($" @{param.Key} = {param.Value ?? "null"}");
}
}
Logger.Error(ex, sb.ToString());
}
public static void LogStoredProcedureStart(string procedureName, List<Parametro> parameters = null)
{
Logger.Debug($"Executing stored procedure: {procedureName}");
if (parameters != null)
{
foreach (var param in parameters)
{
Logger.Debug($" Parameter: {param.ParameterName} = {param.Value ?? "null"}");
}
}
}
public static void LogStoredProcedureSuccess(string procedureName, int rowCount = 0)
{
Logger.Debug($"Stored procedure {procedureName} executed successfully. Rows affected/returned: {rowCount}");
}
public static void LogStoredProcedureError(string procedureName, Exception ex, List<Parametro> parameters = null)
{
Logger.Error(ex, $"Error executing stored procedure {procedureName}");
if (parameters != null)
{
foreach (var param in parameters)
{
Logger.Error($" Parameter: {param.ParameterName} = {param.Value ?? "null"}");
}
}
}
public static void LogError(Exception ex)
{
Logger.Error(ex);
}
public static void LogErrors(Exception ex)
{
Logger.Error(ex);
}
}
}