This commit is contained in:
Janus C. H. Knudsen 2025-03-10 15:56:22 +01:00
parent f3ab94eff1
commit 31666b4ba0
34 changed files with 140 additions and 83 deletions

View file

@ -1,4 +1,4 @@
namespace PlanTempus.Core.Sql.ConnectionFactory
namespace PlanTempus.Core.Database.ConnectionFactory
{
public interface IDbConnectionFactory
{

View file

@ -1,6 +1,7 @@
using Npgsql;
using System.Data;
namespace PlanTempus.Core.Sql.ConnectionFactory
using System.Data;
using Npgsql;
namespace PlanTempus.Core.Database.ConnectionFactory
{
public record ConnectionStringParameters(string User, string Pwd);

View file

@ -2,7 +2,7 @@
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace PlanTempus.Core.Sql;
namespace PlanTempus.Core.Database;
public class DatabaseScope : IDisposable
{
@ -12,6 +12,7 @@ public class DatabaseScope : IDisposable
{
Connection = connection;
_operation = operation;
_operation.Telemetry.Success = true;
}
public IDbConnection Connection { get; }
@ -22,11 +23,6 @@ public class DatabaseScope : IDisposable
Connection.Dispose();
}
public void Success()
{
_operation.Telemetry.Success = true;
}
public void Error(Exception ex)
{
_operation.Telemetry.Success = false;

View file

@ -1,6 +1,6 @@
using System.Data;
namespace PlanTempus.Core.Sql;
namespace PlanTempus.Core.Database;
public interface IDatabaseOperations
{

View file

@ -1,9 +1,9 @@
using System.Data;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using PlanTempus.Core.Sql.ConnectionFactory;
using PlanTempus.Core.Database.ConnectionFactory;
namespace PlanTempus.Core.Sql;
namespace PlanTempus.Core.Database;
public class SqlOperations : IDatabaseOperations
{
@ -32,7 +32,6 @@ public class SqlOperations : IDatabaseOperations
try
{
var result = await operation(scope.Connection);
scope.Success();
return result;
}
catch (Exception ex)
@ -48,7 +47,6 @@ public class SqlOperations : IDatabaseOperations
try
{
await operation(scope.Connection);
scope.Success();
}
catch (Exception ex)
{

View file

@ -1,5 +1,5 @@
using Autofac;
using PlanTempus.Core.Logging;
using PlanTempus.Core.SeqLogging;
namespace PlanTempus.Core.ModuleRegistry
{

View file

@ -10,13 +10,12 @@ namespace PlanTempus.Core.ModuleRegistry
protected override void Load(ContainerBuilder builder)
{
var configuration = TelemetryConfiguration.CreateDefault();
configuration.ConnectionString = TelemetryConfig.ConnectionString;
configuration.TelemetryChannel.DeveloperMode = true;
var client = new Microsoft.ApplicationInsights.TelemetryClient(configuration);
client.Context.GlobalProperties["Application"] = GetType().Namespace.Split('.')[0];
client.Context.GlobalProperties["Application"] = GetType().Namespace?.Split('.')[0];
client.Context.GlobalProperties["MachineName"] = Environment.MachineName;
client.Context.GlobalProperties["Version"] = Environment.Version.ToString();
client.Context.GlobalProperties["ProcessorCount"] = Environment.ProcessorCount.ToString();
@ -34,10 +33,11 @@ namespace PlanTempus.Core.ModuleRegistry
configuration.TelemetryChannel = new Telemetry.SeqTelemetryChannel(messageChannel, client);
}
var telemetryProcessorChain = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(configuration);
var telemetryProcessorChain =
new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(
configuration);
telemetryProcessorChain.Use(next => new Telemetry.Enrichers.EnrichWithMetaTelemetry(next));
telemetryProcessorChain.Build();
}
}
@ -46,4 +46,4 @@ namespace PlanTempus.Core.ModuleRegistry
public string ConnectionString { get; set; }
public bool UseSeqLoggingTelemetryChannel { get; set; }
}
}
}

View file

@ -4,7 +4,7 @@ using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.Hosting;
using PlanTempus.Core.Telemetry;
namespace PlanTempus.Core.Logging
namespace PlanTempus.Core.SeqLogging
{
public class SeqBackgroundService : BackgroundService
{

View file

@ -1,4 +1,4 @@
namespace PlanTempus.Core.Logging
namespace PlanTempus.Core.SeqLogging
{
public record SeqConfiguration(string IngestionEndpoint, string ApiKey, string Environment);
}

View file

@ -1,4 +1,4 @@
namespace PlanTempus.Core.Logging
namespace PlanTempus.Core.SeqLogging
{
public class SeqHttpClient
{

View file

@ -1,13 +1,11 @@
using Microsoft.ApplicationInsights.DataContracts;
using System.Text;
using System.Text;
using Microsoft.ApplicationInsights.DataContracts;
namespace PlanTempus.Core.Logging
namespace PlanTempus.Core.SeqLogging
{
public class SeqLogger<T>
{
private readonly SeqHttpClient _httpClient;
private readonly string _environmentName;
private readonly string _machineName;
private readonly SeqConfiguration _configuration;
public SeqLogger(SeqHttpClient httpClient, SeqConfiguration configuration)
@ -23,8 +21,7 @@ namespace PlanTempus.Core.Logging
{ "@t", trace.Timestamp.UtcDateTime.ToString("o") },
{ "@mt", trace.Message },
{ "@l", MapSeverityToLevel(trace.SeverityLevel) },
{ "Environment", _environmentName },
{ "MachineName", _machineName }
{ "Environment", _configuration.Environment },
};
foreach (var prop in trace.Properties)
@ -43,8 +40,7 @@ namespace PlanTempus.Core.Logging
{ "@t", evt.Timestamp.UtcDateTime.ToString("o") },
{ "@mt", evt.Name },
{ "@l", "Information" },
{ "Environment", _environmentName },
{ "MachineName", _machineName }
{ "Environment", _configuration.Environment }
};
foreach (var prop in evt.Properties)
@ -67,9 +63,8 @@ namespace PlanTempus.Core.Logging
{ "@mt", ex.Exception.Message },
{ "@l", "Error" },
{ "@x", FormatExceptionForSeq(ex.Exception) },
{ "Environment", _environmentName },
{ "MachineName", _machineName },
{ "ExceptionType", ex.Exception.GetType().Name },
{ "Environment", _configuration.Environment },
{ "ExceptionType", ex.Exception.GetType().Name }
};
foreach (var prop in ex.Properties)
@ -88,8 +83,7 @@ namespace PlanTempus.Core.Logging
{ "@t", dep.Timestamp.UtcDateTime.ToString("o") },
{ "@mt", $"Dependency: {dep.Name}" },
{ "@l", dep.Success ?? true ? "Information" : "Error" },
{ "Environment", _environmentName },
{ "MachineName", _machineName },
{ "Environment", _configuration.Environment },
{ "DependencyType", dep.Type },
{ "Target", dep.Target },
{ "Duration", dep.Duration.TotalMilliseconds }