Adds null checks for telemetry and logging configurations Prevents registration of modules with empty configuration Updates test configuration file copy behavior Enhances robustness of test infrastructure by conditionally registering telemetry and logging modules only when valid configuration is present
57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using System.Data;
|
|
using Microsoft.ApplicationInsights;
|
|
using Microsoft.ApplicationInsights.DataContracts;
|
|
using SWP.Core.Database.ConnectionFactory;
|
|
|
|
namespace SWP.Core.Database;
|
|
|
|
public class SqlOperations : IDatabaseOperations
|
|
{
|
|
private readonly IDbConnectionFactory _connectionFactory;
|
|
private readonly TelemetryClient _telemetryClient;
|
|
|
|
public SqlOperations(IDbConnectionFactory connectionFactory, TelemetryClient telemetryClient = null)
|
|
{
|
|
_connectionFactory = connectionFactory;
|
|
_telemetryClient = telemetryClient ?? new TelemetryClient();
|
|
}
|
|
|
|
public DatabaseScope CreateScope(string operationName)
|
|
{
|
|
var connection = _connectionFactory.Create();
|
|
var operation = _telemetryClient.StartOperation<DependencyTelemetry>(operationName);
|
|
operation.Telemetry.Type = "SQL";
|
|
operation.Telemetry.Target = "PostgreSQL";
|
|
|
|
return new DatabaseScope(connection, operation);
|
|
}
|
|
|
|
public async Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName)
|
|
{
|
|
using var scope = CreateScope(operationName);
|
|
try
|
|
{
|
|
var result = await operation(scope.Connection);
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
scope.Error(ex);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName)
|
|
{
|
|
using var scope = CreateScope(operationName);
|
|
try
|
|
{
|
|
await operation(scope.Connection);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
scope.Error(ex);
|
|
throw;
|
|
}
|
|
}
|
|
}
|