using System; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Collections.Generic; using System.Configuration; namespace DataAccessLayer { public static class SqlLogging { private static readonly string ConnectionString; private static readonly object lockObj = new object(); static SqlLogging() { ConnectionString = ConfigurationManager.ConnectionStrings["SqlServerStampeC6Connection"]?.ConnectionString; if (string.IsNullOrEmpty(ConnectionString)) { throw new ConfigurationErrorsException("SqlServerStampeC6Connection connection string not found in configuration."); } } private static void WriteToLogN(string level, string message, string logger, string callSite, string exception = null) { try { using (var conn = new SqlConnection(ConnectionString)) using (var cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = @"INSERT INTO [dbo].LogN ([Logged], [Level], [Message], [Logger], [CallSite], [Exception], [Application]) VALUES (GETDATE(), @level, @message, @logger, @callSite, @exception, @application)"; cmd.Parameters.AddWithValue("@level", level); cmd.Parameters.AddWithValue("@message", message); cmd.Parameters.AddWithValue("@logger", logger); cmd.Parameters.AddWithValue("@callSite", callSite); cmd.Parameters.AddWithValue("@exception", (object)exception ?? DBNull.Value); cmd.Parameters.AddWithValue("@application", "SqlLogging"); cmd.ExecuteNonQuery(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error writing to LogN: {ex.Message}"); } } public static void LogStoredProcedure(string spName, Dictionary parameters = null, long executionTimeMs = 0) { try { var logBuilder = new StringBuilder(); logBuilder.AppendLine($"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}"); } } logBuilder.AppendLine($"Execution Time: {executionTimeMs}ms"); WriteToLogN( "Info", logBuilder.ToString(), "SqlLogging", $"LogStoredProcedure({spName})" ); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error logging stored procedure: {ex.Message}"); WriteToLogN("Error", ex.Message, "SqlLogging", "LogStoredProcedure", ex.ToString()); } } public static void LogCommand(SqlCommand command, long executionTimeMs = 0) { try { var logBuilder = new StringBuilder(); 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}"); } } logBuilder.AppendLine($"Execution Time: {executionTimeMs}ms"); WriteToLogN( "Info", logBuilder.ToString(), "SqlLogging", $"LogCommand({command.CommandType})" ); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error logging SQL command: {ex.Message}"); WriteToLogN("Error", ex.Message, "SqlLogging", "LogCommand", ex.ToString()); } } } }