Aggiunta log delle sql eseguite
This commit is contained in:
parent
4c6929b5e4
commit
1746bf8e8a
@ -4,7 +4,7 @@
|
||||
sqlcommandtimeout=200
|
||||
|
||||
! ambiente {SVILUPPO, TEST, COLLAUDO, PRODUZIONE}
|
||||
ambiente=PRODUZIONE
|
||||
ambiente=SVILUPPO
|
||||
|
||||
! Licenza DynamicPDF
|
||||
licenza=MER50NESAOENJBo5BnJKfSZXJj/qdgajtuOAxakMA4HW7vmqjo4/RyQDP2DdxyHzZLmu8zCtUkxdEgp58zADbGe4FYS7SC83+vvg
|
||||
|
@ -27,6 +27,6 @@
|
||||
</target>
|
||||
</targets>
|
||||
<rules>
|
||||
<logger name="*" minlevel="Debug" writeTo="file" />
|
||||
<logger name="*" minlevel="Debug" writeTo="database" />
|
||||
</rules>
|
||||
</nlog>
|
@ -19,6 +19,7 @@ namespace DataAccessLayer
|
||||
{
|
||||
public class DataAccess
|
||||
{
|
||||
|
||||
public static DataTable ExecuteDataTableStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
|
||||
{
|
||||
Database db = GetDatabaseObject(_dbprovider);
|
||||
@ -32,12 +33,15 @@ namespace DataAccessLayer
|
||||
}
|
||||
try
|
||||
{
|
||||
return db.ExecuteDataSet(comm).Tables[0];
|
||||
SqlLogging.LogStoredProcedureStart(nomeStoredProcedure, paramList);
|
||||
var result = db.ExecuteDataSet(comm).Tables[0];
|
||||
SqlLogging.LogStoredProcedureSuccess(nomeStoredProcedure, result.Rows.Count);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogStoredProcedureError(nomeStoredProcedure, ex, paramList);
|
||||
DataBaseException ecc = new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
||||
logger.Error(ex);
|
||||
throw ecc;
|
||||
}
|
||||
}
|
||||
@ -70,7 +74,7 @@ namespace DataAccessLayer
|
||||
//logger.Error(db.ConnectionStringWithoutCredentials);
|
||||
//logger.Error(nomeStoredProcedure);
|
||||
DataBaseException ecc = new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
||||
logger.Errors(ex);
|
||||
SqlLogging.LogErrors(ex);
|
||||
throw ecc;
|
||||
}
|
||||
}
|
||||
@ -98,7 +102,7 @@ namespace DataAccessLayer
|
||||
catch (Exception ex)
|
||||
{
|
||||
DataBaseException ecc = new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
||||
logger.Errors(ex);
|
||||
SqlLogging.LogErrors(ex);
|
||||
throw ecc;
|
||||
}
|
||||
}
|
||||
@ -151,7 +155,7 @@ namespace DataAccessLayer
|
||||
{
|
||||
|
||||
transaction.Rollback();
|
||||
logger.Errors(ex);
|
||||
SqlLogging.LogErrors(ex);
|
||||
//throw new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
||||
return ritorno;
|
||||
}
|
||||
|
@ -1,271 +0,0 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.OracleClient;
|
||||
using System.Data.Common;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Practices.EnterpriseLibrary.Data;
|
||||
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
|
||||
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
|
||||
|
||||
/*
|
||||
namespace DataAccessLayer
|
||||
{
|
||||
/// <summary>
|
||||
/// DataAccess costruito solo per ConsulenzaEvoluta. A differenza degli altri DB quello di CE è stato impostato con IsolationLevel = Snapshot.
|
||||
/// </summary>
|
||||
public class DataAccessCE
|
||||
{
|
||||
/// <summary>
|
||||
/// Ritorna un DataTable lanciando la stored con i parametri passati in input sul database di ConsulenzaEvoluta.
|
||||
/// IsolationLevel impostato a Snapshot
|
||||
/// </summary>
|
||||
/// <param name="_dbprovider"></param>
|
||||
/// <param name="nomeStoredProcedure"></param>
|
||||
/// <param name="paramList"></param>
|
||||
/// <returns></returns>
|
||||
public static DataTable ExecuteDataTableStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
|
||||
{
|
||||
// The DataTable to be ret urned
|
||||
Database db = GetDatabaseObject(_dbprovider);
|
||||
|
||||
using (DbConnection connection = db.CreateConnection())
|
||||
{
|
||||
connection.Open();
|
||||
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.Snapshot);
|
||||
try
|
||||
{
|
||||
// Execute the command making sure the connection gets closed in the end
|
||||
DbCommand comm = db.GetStoredProcCommand(nomeStoredProcedure);
|
||||
comm.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
if (paramList != null)
|
||||
{
|
||||
foreach (Parametro param in paramList)
|
||||
db.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
|
||||
}
|
||||
|
||||
DataTable table = db.ExecuteDataSet(comm, transaction).Tables[0];
|
||||
table.TableName = "TABLENAME";
|
||||
return table;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ritorna un DataSet lanciando la stored con i parametri passati in input sul database di ConsulenzaEvoluta.
|
||||
/// IsolationLevel impostato a Snapshot
|
||||
/// </summary>
|
||||
/// <param name="_dbprovider"></param>
|
||||
/// <param name="nomeStoredProcedure"></param>
|
||||
/// <param name="paramList"></param>
|
||||
/// <returns></returns>
|
||||
public static DataSet ExecuteDataSetStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
|
||||
{
|
||||
Database db = GetDatabaseObject(_dbprovider);
|
||||
|
||||
using (DbConnection connection = db.CreateConnection())
|
||||
{
|
||||
connection.Open();
|
||||
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.Snapshot);
|
||||
try
|
||||
{
|
||||
// Execute the command making sure the connection gets closed in the end
|
||||
DbCommand comm = db.GetStoredProcCommand(nomeStoredProcedure);
|
||||
comm.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
if (paramList != null)
|
||||
{
|
||||
foreach (Parametro param in paramList)
|
||||
db.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
|
||||
}
|
||||
|
||||
// Execute the command making sure the connection gets closed in the end
|
||||
return db.ExecuteDataSet(comm, transaction);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recupera un valore intero eseguendo la stored con i parametri passati in input sul database di ConsulenzaEvoluta.
|
||||
/// IsolationLevel impostato a Snapshot
|
||||
/// </summary>
|
||||
/// <param name="_dbprovider"></param>
|
||||
/// <param name="nomeStoredProcedure"></param>
|
||||
/// <param name="paramList"></param>
|
||||
/// <returns></returns>
|
||||
public static object ExecuteScalarStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
|
||||
{
|
||||
Database db = GetDatabaseObject(_dbprovider);
|
||||
|
||||
using (DbConnection connection = db.CreateConnection())
|
||||
{
|
||||
connection.Open();
|
||||
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.Snapshot);
|
||||
try
|
||||
{
|
||||
DbCommand comm = db.GetStoredProcCommand(nomeStoredProcedure);
|
||||
if (paramList != null)
|
||||
{
|
||||
foreach (Parametro param in paramList)
|
||||
db.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
|
||||
}
|
||||
|
||||
return db.ExecuteScalar(comm, transaction);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Esegue gli statement sul database di ConsulenzaEvoluta aggiunti alla collezione listaSqlStatement in modalità transazionale.
|
||||
/// IsolationLevel impostato a Snapshot
|
||||
/// </summary>
|
||||
/// <param name="_dbprovider"></param>
|
||||
/// <param name="_transactionStatement"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ExecuteNonQueryMultipleTransaction(DBProvider _dbprovider, List<TransactionStatement> _transactionStatement)
|
||||
{
|
||||
bool result = true;
|
||||
string _sqlstatement = "";
|
||||
List<Parametro> _paramlist = null;
|
||||
DbCommand command = null;
|
||||
// The DataTable to be ret urned
|
||||
Database db = GetDatabaseObject(_dbprovider);
|
||||
|
||||
using (DbConnection connection = db.CreateConnection())
|
||||
{
|
||||
connection.Open();
|
||||
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.Snapshot);
|
||||
|
||||
try
|
||||
{
|
||||
foreach (TransactionStatement _trans in _transactionStatement)
|
||||
{
|
||||
_sqlstatement = _trans.SqlStatement;
|
||||
_paramlist = _trans.ListaParametro;
|
||||
// Esegue la transazione.
|
||||
|
||||
if (_trans.StatementType == CommandType.Text)
|
||||
command = db.GetSqlStringCommand(_trans.SqlStatement);
|
||||
else if (_trans.StatementType == CommandType.StoredProcedure)
|
||||
command = db.GetStoredProcCommand(_trans.SqlStatement);
|
||||
|
||||
foreach (Parametro param in _trans.ListaParametro)
|
||||
db.AddParameter(command, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
|
||||
|
||||
db.ExecuteNonQuery(command, transaction);
|
||||
}
|
||||
// Commit the transaction.
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Roll back the transaction.
|
||||
transaction.Rollback();
|
||||
throw new DataBaseException(ex, db.ConnectionStringWithoutCredentials, _sqlstatement, _paramlist);
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Esegue la stored con i parametri passati in input sul database di ConsulenzaEvoluta.
|
||||
/// IsolationLevel impostato a Snapshot
|
||||
/// </summary>
|
||||
/// <param name="_dbprovider"></param>
|
||||
/// <param name="nomeStoredProcedure"></param>
|
||||
/// <param name="paramList"></param>
|
||||
/// <returns></returns>
|
||||
public static int ExecuteNonQueryStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
|
||||
{
|
||||
Database db = GetDatabaseObject(_dbprovider);
|
||||
|
||||
int ritorno = -1;
|
||||
|
||||
using (DbConnection connection = db.CreateConnection())
|
||||
{
|
||||
connection.Open();
|
||||
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.Snapshot);
|
||||
try
|
||||
{
|
||||
// Execute the command making sure the connection gets closed in the end
|
||||
DbCommand comm = db.GetStoredProcCommand(nomeStoredProcedure);
|
||||
if (paramList != null)
|
||||
{
|
||||
foreach (Parametro param in paramList)
|
||||
db.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
|
||||
}
|
||||
|
||||
Parametro returnParam = new Parametro();
|
||||
returnParam.ParameterName = "RETURN";
|
||||
returnParam.DbType = DbType.Int32;
|
||||
returnParam.Direction = ParameterDirection.ReturnValue;
|
||||
returnParam.Value = -1;
|
||||
|
||||
db.AddParameter(comm, returnParam.ParameterName, returnParam.DbType, returnParam.Direction, returnParam.SourceColumn, returnParam.SourceVersion, returnParam.Value);
|
||||
|
||||
ritorno = db.ExecuteNonQuery(comm, transaction);
|
||||
ritorno = (Int32)comm.Parameters["@RETURN"].Value;
|
||||
|
||||
//Commit the transaction
|
||||
transaction.Commit();
|
||||
return ritorno;
|
||||
}
|
||||
catch (DataBaseException ex)
|
||||
{
|
||||
//Roll back the transaction.
|
||||
transaction.Rollback();
|
||||
throw new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recupera la stringa di connessione per il database di ConsulenzaEvoluta
|
||||
/// </summary>
|
||||
/// <param name="_dbprovider"></param>
|
||||
/// <returns></returns>
|
||||
private static Database GetDatabaseObject(DBProvider _dbprovider)
|
||||
{
|
||||
string connstring = WebConfigParameter.getConnectionString("SqlServerConsulenzaEvolutaConnection");
|
||||
return new SqlDatabase(connstring);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
@ -1,19 +0,0 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data.OracleClient;
|
||||
using System.Data.Common;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Practices.EnterpriseLibrary.Data;
|
||||
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
|
||||
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Configuration;
|
||||
using NLog;
|
||||
|
||||
namespace DataAccessLayer
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -105,6 +105,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CACHE.cs" />
|
||||
<Compile Include="SqlLogging.cs" />
|
||||
<Compile Include="TemplateProvider.cs" />
|
||||
<Compile Include="Config.cs" />
|
||||
<Compile Include="DataAccess.cs" />
|
||||
|
32
root/Shared/DataAccessLayer/NLog.config
Normal file
32
root/Shared/DataAccessLayer/NLog.config
Normal file
@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
|
||||
autoReload="true"
|
||||
throwExceptions="true"
|
||||
internalLogLevel="Error" internalLogFile="c:\temp\nlog-internal.log" >
|
||||
<targets>
|
||||
<target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
|
||||
<target xsi:type="File" fileName="${basedir}/logs/${level}.txt" />
|
||||
</target>
|
||||
<target name="database"
|
||||
xsi:type="Database"
|
||||
connectionStringName="SqlServerStampeC6Connection"
|
||||
commandText=" INSERT INTO [dbo].LogN ([Logged], [Level], [Message], [Logger], [CallSite],
|
||||
[Exception], [Application])
|
||||
VALUES (GETDATE(), @level, @message, @logger, @callSite, @exception, @application);">
|
||||
<parameter name="@application" layout="ContrattoSei" />
|
||||
<parameter name="@machineName" layout="${machinename}" />
|
||||
<parameter name="@logged" layout="${date}" />
|
||||
<parameter name="@level" layout="${level}" />
|
||||
<parameter name="@message" layout="${message}" />
|
||||
<parameter name="@logger" layout="${logger}" />
|
||||
<parameter name="@properties" layout="${all-event-properties:separator=|}" />
|
||||
<parameter name="@callSite" layout="${callsite}" />
|
||||
<parameter name="@exception" layout="${exception:tostring}" />
|
||||
</target>
|
||||
</targets>
|
||||
<rules>
|
||||
<logger name="*" minlevel="Debug" writeTo="database" />
|
||||
</rules>
|
||||
</nlog>
|
@ -1,35 +0,0 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace DataAccessLayer
|
||||
{
|
||||
public static class SqlCommandExtensions
|
||||
{
|
||||
public static int ExecuteNonQueryWithLogging(this SqlCommand command)
|
||||
{
|
||||
var startTime = DateTime.Now;
|
||||
var result = command.ExecuteNonQuery();
|
||||
var executionTime = (long)(DateTime.Now - startTime).TotalMilliseconds;
|
||||
SqlLogging.LogCommand(command, executionTime);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static SqlDataReader ExecuteReaderWithLogging(this SqlCommand command)
|
||||
{
|
||||
var startTime = DateTime.Now;
|
||||
var result = command.ExecuteReader();
|
||||
var executionTime = (long)(DateTime.Now - startTime).TotalMilliseconds;
|
||||
SqlLogging.LogCommand(command, executionTime);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static object ExecuteScalarWithLogging(this SqlCommand command)
|
||||
{
|
||||
var startTime = DateTime.Now;
|
||||
var result = command.ExecuteScalar();
|
||||
var executionTime = (long)(DateTime.Now - startTime).TotalMilliseconds;
|
||||
SqlLogging.LogCommand(command, executionTime);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,128 +1,87 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using System.Text;
|
||||
|
||||
namespace DataAccessLayer
|
||||
{
|
||||
public static class SqlLogging
|
||||
{
|
||||
private static readonly string ConnectionString;
|
||||
private static readonly object lockObj = new object();
|
||||
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
static SqlLogging()
|
||||
public static void LogSqlStart(string sql, Dictionary<string, object> parameters = null)
|
||||
{
|
||||
ConnectionString = ConfigurationManager.ConnectionStrings["SqlServerStampeC6Connection"]?.ConnectionString;
|
||||
if (string.IsNullOrEmpty(ConnectionString))
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine($"Executing SQL: {sql}");
|
||||
|
||||
if (parameters != null && parameters.Any())
|
||||
{
|
||||
throw new ConfigurationErrorsException("SqlServerStampeC6Connection connection string not found in configuration.");
|
||||
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"}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteToLogN(string level, string message, string logger, string callSite, string exception = null)
|
||||
public static void LogStoredProcedureSuccess(string procedureName, int rowCount = 0)
|
||||
{
|
||||
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");
|
||||
Logger.Debug($"Stored procedure {procedureName} executed successfully. Rows affected/returned: {rowCount}");
|
||||
}
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
public static void LogStoredProcedureError(string procedureName, Exception ex, List<Parametro> parameters = null)
|
||||
{
|
||||
Logger.Error(ex, $"Error executing stored procedure {procedureName}");
|
||||
if (parameters != null)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error writing to LogN: {ex.Message}");
|
||||
foreach (var param in parameters)
|
||||
{
|
||||
Logger.Error($" Parameter: {param.ParameterName} = {param.Value ?? "null"}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void LogStoredProcedure(string spName, Dictionary<string, object> parameters = null, long executionTimeMs = 0)
|
||||
public static void LogError(Exception ex)
|
||||
{
|
||||
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());
|
||||
}
|
||||
Logger.Error(ex);
|
||||
}
|
||||
|
||||
public static void LogCommand(SqlCommand command, long executionTimeMs = 0)
|
||||
public static void LogErrors(Exception ex)
|
||||
{
|
||||
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());
|
||||
}
|
||||
Logger.Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
@ -131,8 +131,7 @@ namespace DataAccessLayer
|
||||
|
||||
public DataTable executeQuery(string Sql, List<SqlParameter> Params = null, string nomedt = "data", CommandType TipoEsecuzione = CommandType.Text)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(talking_code))
|
||||
logger.Debugs(talking_code + ":" + Sql);
|
||||
SqlLogging.LogSqlStart(Sql, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
|
||||
DataTable result = null;
|
||||
|
||||
@ -170,6 +169,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
finally
|
||||
@ -188,6 +188,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
}
|
||||
@ -197,8 +198,7 @@ namespace DataAccessLayer
|
||||
|
||||
public DataSet executeProcedure(string Sql, List<SqlParameter> Params, string nomeds = "data")
|
||||
{
|
||||
if (!string.IsNullOrEmpty(talking_code))
|
||||
logger.Debugs(talking_code + ":" + Sql);
|
||||
SqlLogging.LogSqlStart(Sql, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
|
||||
DataSet result = null;
|
||||
try
|
||||
@ -233,6 +233,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
finally
|
||||
@ -251,6 +252,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
return result;
|
||||
@ -258,8 +260,7 @@ namespace DataAccessLayer
|
||||
|
||||
public DataTable GetDataTableFromProcedure(string Sql, List<SqlParameter> Params = null, string nomedt = "data")
|
||||
{
|
||||
if (!string.IsNullOrEmpty(talking_code))
|
||||
logger.Debugs(talking_code + ":" + Sql);
|
||||
SqlLogging.LogSqlStart(Sql, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
|
||||
DataTable result = null;
|
||||
try
|
||||
@ -294,6 +295,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception et)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, et, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = et;
|
||||
}
|
||||
finally
|
||||
@ -309,6 +311,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
return result;
|
||||
@ -326,8 +329,7 @@ namespace DataAccessLayer
|
||||
|
||||
public object executeScalar(string Sql, List<SqlParameter> Params = null, CommandType cmdtype = CommandType.StoredProcedure)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(talking_code))
|
||||
logger.Debugs(talking_code + ":" + Sql);
|
||||
SqlLogging.LogSqlStart(Sql, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
|
||||
object result = null;
|
||||
try
|
||||
@ -355,6 +357,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
finally
|
||||
@ -369,6 +372,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
|
||||
@ -377,8 +381,7 @@ namespace DataAccessLayer
|
||||
|
||||
public object executeScalarWT(string Sql, List<SqlParameter> Params = null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(talking_code))
|
||||
logger.Debugs(talking_code + ":" + Sql);
|
||||
SqlLogging.LogSqlStart(Sql, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
|
||||
object result = null;
|
||||
try
|
||||
@ -406,6 +409,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
finally
|
||||
@ -422,6 +426,7 @@ namespace DataAccessLayer
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SqlLogging.LogSqlError(Sql, ex, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
SqlException = ex;
|
||||
}
|
||||
|
||||
@ -430,8 +435,7 @@ namespace DataAccessLayer
|
||||
|
||||
public Int32 executeNonQueryWT(string Sql, List<SqlParameter> Params = null, CommandType TipoEsecuzione = CommandType.StoredProcedure)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(talking_code))
|
||||
logger.Debugs(talking_code + ":" + Sql);
|
||||
SqlLogging.LogSqlStart(Sql, Params?.ToDictionary(p => p.ParameterName, p => p.Value));
|
||||
|
||||
Int32 result = 0;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user