PlanTempusApp/PlanTempus.Core/Database/DatabaseScope.cs

38 lines
1 KiB
C#
Raw Normal View History

2025-03-04 23:54:55 +01:00
using System.Data;
2025-03-11 00:28:06 +01:00
using System.Diagnostics;
2025-03-04 23:54:55 +01:00
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
2025-03-10 15:56:22 +01:00
namespace PlanTempus.Core.Database;
2025-03-04 23:54:55 +01:00
public class DatabaseScope : IDisposable
{
2025-03-11 00:28:06 +01:00
internal readonly IOperationHolder<DependencyTelemetry> _operation;
private readonly Stopwatch _stopwatch;
2025-03-04 23:54:55 +01:00
public DatabaseScope(IDbConnection connection, IOperationHolder<DependencyTelemetry> operation)
{
Connection = connection;
_operation = operation;
2025-03-10 15:56:22 +01:00
_operation.Telemetry.Success = true;
2025-03-12 00:13:53 +01:00
_operation.Telemetry.Timestamp = DateTimeOffset.UtcNow;
2025-03-11 00:28:06 +01:00
_stopwatch = Stopwatch.StartNew();
2025-03-04 23:54:55 +01:00
}
public IDbConnection Connection { get; }
public void Dispose()
{
2025-03-11 00:28:06 +01:00
_stopwatch.Stop();
_operation.Telemetry.Duration = _stopwatch.Elapsed;
2025-03-04 23:54:55 +01:00
_operation.Dispose();
Connection.Dispose();
}
public void Error(Exception ex)
{
_operation.Telemetry.Success = false;
_operation.Telemetry.Properties["Error"] = ex.Message;
}
}