Enhancing telemetry in Seq and fixes namespaces
This commit is contained in:
parent
5568007237
commit
9f4996bc8f
65 changed files with 1122 additions and 1139 deletions
|
|
@ -4,7 +4,7 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core.Configurations.Common
|
||||
namespace PlanTempus.Core.Configurations.Common
|
||||
{
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
using Newtonsoft.Json.Linq;
|
||||
namespace Core.Configurations
|
||||
namespace PlanTempus.Core.Configurations
|
||||
{
|
||||
public interface IConfigurationBuilder
|
||||
{
|
||||
|
|
@ -133,7 +133,7 @@ namespace Core.Configurations
|
|||
public interface IConfigurationProvider
|
||||
{
|
||||
void Build();
|
||||
Newtonsoft.Json.Linq.JObject Configuration();
|
||||
JObject Configuration();
|
||||
}
|
||||
|
||||
public class ConfigurationSection : IConfigurationSection
|
||||
|
|
@ -148,6 +148,6 @@ namespace Core.Configurations
|
|||
{
|
||||
string Path { get; }
|
||||
string Key { get; }
|
||||
Newtonsoft.Json.Linq.JToken Value { get; set; }
|
||||
JToken Value { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
namespace Core.Configurations
|
||||
namespace PlanTempus.Core.Configurations
|
||||
{
|
||||
/// <summary>
|
||||
/// Marker interface for application configurations that should be automatically registered in the DI container.
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
namespace Core.Configurations
|
||||
namespace PlanTempus.Core.Configurations
|
||||
{
|
||||
public interface IConfigurationRoot : IConfiguration { }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
using Core.Exceptions;
|
||||
using PlanTempus.Core.Exceptions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using PlanTempus.Core.Configurations;
|
||||
|
||||
namespace Core.Configurations.JsonConfigProvider
|
||||
namespace PlanTempus.Core.Configurations.JsonConfigProvider
|
||||
{
|
||||
public static class JsonConfigExtension
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
namespace Core.Configurations.SmartConfigProvider;
|
||||
namespace PlanTempus.Core.Configurations.SmartConfigProvider;
|
||||
public class AppConfiguration
|
||||
{
|
||||
public long Id { get; set; }
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
namespace Core.Configurations.SmartConfigProvider;
|
||||
namespace PlanTempus.Core.Configurations.SmartConfigProvider;
|
||||
public interface IConfigurationRepository
|
||||
{
|
||||
string ConnectionString { get; set; }
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
using System.Data;
|
||||
using Insight.Database;
|
||||
|
||||
namespace Core.Configurations.SmartConfigProvider.Repositories;
|
||||
namespace PlanTempus.Core.Configurations.SmartConfigProvider.Repositories;
|
||||
public class PostgresConfigurationRepository : IConfigurationRepository
|
||||
{
|
||||
private IDbConnection _connection;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
namespace Core.Configurations.SmartConfigProvider
|
||||
using PlanTempus.Core.Configurations;
|
||||
|
||||
namespace PlanTempus.Core.Configurations.SmartConfigProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for adding smart configuration providers to IConfigurationBuilder.
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
namespace Core.Configurations.SmartConfigProvider
|
||||
namespace PlanTempus.Core.Configurations.SmartConfigProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration options for setting up smart configuration providers.
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
using Core.Configurations.JsonConfigProvider;
|
||||
using Core.Exceptions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using PlanTempus.Core.Configurations.JsonConfigProvider;
|
||||
using PlanTempus.Core.Exceptions;
|
||||
|
||||
namespace Core.Configurations.SmartConfigProvider
|
||||
namespace PlanTempus.Core.Configurations.SmartConfigProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration provider that loads configuration from a smart configuration source (e.g. database).
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core.Entities.Users
|
||||
namespace PlanTempus.Core.Entities.Users
|
||||
{
|
||||
public class User
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
namespace Core.Entities.Users
|
||||
namespace PlanTempus.Core.Entities.Users
|
||||
{
|
||||
public static class PasswordHasher
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
namespace Core.Exceptions
|
||||
namespace PlanTempus.Core.Exceptions
|
||||
{
|
||||
internal class ConfigurationException : Exception
|
||||
{
|
||||
public ConfigurationException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
internal class ConfigurationException : Exception
|
||||
{
|
||||
public ConfigurationException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
using Core.Telemetry;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using PlanTempus.Core.Telemetry;
|
||||
|
||||
namespace Core.Logging
|
||||
namespace PlanTempus.Core.Logging
|
||||
{
|
||||
public class SeqBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IMessageChannel<ITelemetry> _messageChannel;
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
private readonly SeqLogger _seqLogger;
|
||||
private readonly SeqLogger<SeqBackgroundService> _seqLogger;
|
||||
|
||||
public SeqBackgroundService(TelemetryClient telemetryClient,
|
||||
IMessageChannel<ITelemetry> messageChannel,
|
||||
SeqLogger seqlogger)
|
||||
SeqLogger<SeqBackgroundService> seqlogger)
|
||||
{
|
||||
_telemetryClient = telemetryClient;
|
||||
_messageChannel = messageChannel;
|
||||
|
|
@ -49,12 +49,12 @@ namespace Core.Logging
|
|||
await _seqLogger.LogAsync(et);
|
||||
break;
|
||||
|
||||
case EventTelemetry et:
|
||||
await _seqLogger.LogAsync(et);
|
||||
break;
|
||||
case EventTelemetry et:
|
||||
await _seqLogger.LogAsync(et);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
default:
|
||||
throw new NotSupportedException(telemetry.GetType().Name);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using System.Text;
|
||||
|
||||
namespace Core.Logging
|
||||
namespace PlanTempus.Core.Logging
|
||||
{
|
||||
public record SeqConfiguration(string IngestionEndpoint, string ApiKey, string Environment);
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
namespace Core.Logging
|
||||
namespace PlanTempus.Core.Logging
|
||||
{
|
||||
public class SeqHttpClient
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,248 +1,239 @@
|
|||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using System.Text;
|
||||
|
||||
namespace Core.Logging
|
||||
namespace PlanTempus.Core.Logging
|
||||
{
|
||||
public class SeqLogger
|
||||
{
|
||||
private readonly SeqHttpClient _httpClient;
|
||||
private readonly string _environmentName;
|
||||
private readonly string _machineName;
|
||||
private readonly SeqConfiguration _configuration;
|
||||
public class SeqLogger<T>
|
||||
{
|
||||
private readonly SeqHttpClient _httpClient;
|
||||
private readonly string _environmentName;
|
||||
private readonly string _machineName;
|
||||
private readonly SeqConfiguration _configuration;
|
||||
|
||||
public SeqLogger(SeqHttpClient httpClient, string environmentName, SeqConfiguration configuration)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_environmentName = configuration.Environment;
|
||||
_machineName = Environment.MachineName;
|
||||
}
|
||||
public SeqLogger(SeqHttpClient httpClient, string environmentName, SeqConfiguration configuration)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task LogAsync(TraceTelemetry trace, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", trace.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt", trace.Message },
|
||||
{ "@l", MapSeverityToLevel(trace.SeverityLevel) },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName }
|
||||
};
|
||||
public async Task LogAsync(TraceTelemetry trace, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", trace.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt", trace.Message },
|
||||
{ "@l", MapSeverityToLevel(trace.SeverityLevel) },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName }
|
||||
};
|
||||
|
||||
foreach (var prop in trace.Properties)
|
||||
{
|
||||
seqEvent.Add(prop.Key, prop.Value);
|
||||
}
|
||||
foreach (var prop in trace.Properties)
|
||||
seqEvent.Add($"prop_{prop.Key}", prop.Value);
|
||||
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
foreach (var prop in trace.Context.GlobalProperties)
|
||||
seqEvent.Add($"global_{prop.Key}", prop.Value);
|
||||
|
||||
public async Task LogAsync(EventTelemetry evt, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", evt.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt", evt.Name },
|
||||
{ "@l", "Information" },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName }
|
||||
};
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
|
||||
foreach (var prop in evt.Properties)
|
||||
{
|
||||
seqEvent.Add(prop.Key, prop.Value);
|
||||
}
|
||||
public async Task LogAsync(EventTelemetry evt, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", evt.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt", evt.Name },
|
||||
{ "@l", "Information" },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName }
|
||||
};
|
||||
|
||||
foreach (var metric in evt.Metrics)
|
||||
{
|
||||
seqEvent.Add($"metric_{metric.Key}", metric.Value);
|
||||
}
|
||||
foreach (var prop in evt.Properties)
|
||||
seqEvent.Add($"prop_{prop.Key}", prop.Value);
|
||||
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
foreach (var prop in evt.Context.GlobalProperties)
|
||||
seqEvent.Add($"global_{prop.Key}", prop.Value);
|
||||
|
||||
public async Task LogAsync(ExceptionTelemetry ex, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", ex.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt", ex.Exception.Message },
|
||||
{ "@l", "Error" },
|
||||
{ "@x", FormatExceptionForSeq(ex.Exception) },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName },
|
||||
{ "ExceptionType", ex.Exception.GetType().Name },
|
||||
};
|
||||
foreach (var metric in evt.Metrics)
|
||||
seqEvent.Add($"metric_{metric.Key}", metric.Value);
|
||||
|
||||
foreach (var prop in ex.Properties)
|
||||
{
|
||||
seqEvent.Add(prop.Key, prop.Value);
|
||||
}
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
public async Task LogAsync(ExceptionTelemetry ex, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", ex.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt", ex.Exception.Message },
|
||||
{ "@l", "Error" },
|
||||
{ "@x", FormatExceptionForSeq(ex.Exception) },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName },
|
||||
{ "ExceptionType", ex.Exception.GetType().Name },
|
||||
};
|
||||
|
||||
public async Task LogAsync(DependencyTelemetry dep, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", dep.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt", $"Dependency: {dep.Name}" },
|
||||
{ "@l", dep.Success??true ? "Information" : "Error" },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName },
|
||||
{ "DependencyType", dep.Type },
|
||||
{ "Target", dep.Target },
|
||||
{ "Duration", dep.Duration.TotalMilliseconds }
|
||||
};
|
||||
foreach (var prop in ex.Properties)
|
||||
seqEvent.Add($"prop_{prop.Key}", prop.Value);
|
||||
|
||||
foreach (var prop in dep.Properties)
|
||||
{
|
||||
seqEvent.Add(prop.Key, prop.Value);
|
||||
}
|
||||
foreach (var prop in ex.Context.GlobalProperties)
|
||||
seqEvent.Add($"global_{prop.Key}", prop.Value);
|
||||
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task LogAsync(RequestTelemetry req, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", req.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt",$"{req.Properties["httpMethod"]} {req.Name}" },
|
||||
{ "@l", req.Success??true ? "Information" : "Error" },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName },
|
||||
{ "Url", req.Url },
|
||||
{ "ResponseCode", req.ResponseCode },
|
||||
{ "Application", "sadasd" },
|
||||
{ "RequestMethod", req.Properties["httpMethod"] },
|
||||
{ "RequestId", req.Id },
|
||||
{ "ItemTypeFlag", req.ItemTypeFlag.ToString() },
|
||||
{ "@sp", "12"},
|
||||
{ "@tr", "23344"},
|
||||
{ "@sk","Server" },
|
||||
{ "@st", req.Timestamp.UtcDateTime.Subtract(req.Duration).ToString("o") }
|
||||
};
|
||||
req.Properties.Remove("httpMethod");
|
||||
public async Task LogAsync(DependencyTelemetry dep, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", dep.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt", $"Dependency: {dep.Name}" },
|
||||
{ "@l", dep.Success??true ? "Information" : "Error" },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName },
|
||||
{ "DependencyType", dep.Type },
|
||||
{ "Target", dep.Target },
|
||||
{ "Duration", dep.Duration.TotalMilliseconds }
|
||||
};
|
||||
|
||||
//we should add a property with name { "Application", "<...>" } other the Seq Span is not looking ok
|
||||
foreach (var prop in req.Properties)
|
||||
{
|
||||
seqEvent.Add(prop.Key, prop.Value);
|
||||
}
|
||||
foreach (var prop in dep.Properties)
|
||||
seqEvent.Add($"prop_{prop.Key}", prop.Value);
|
||||
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
public async Task LogAsync(Microsoft.ApplicationInsights.Extensibility.IOperationHolder<RequestTelemetry> operationHolder, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var req = operationHolder.Telemetry;
|
||||
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
foreach (var prop in dep.Context.GlobalProperties)
|
||||
seqEvent.Add($"global_{prop.Key}", prop.Value);
|
||||
|
||||
{ "@t", req.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt",$"{req.Properties["httpMethod"]} {req.Name}" },
|
||||
{ "@l", req.Success??true ? "Information" : "Error" },
|
||||
{ "Environment", _environmentName },
|
||||
{ "MachineName", _machineName },
|
||||
{ "Url", req.Url },
|
||||
{ "ResponseCode", req.ResponseCode },
|
||||
{ "Application", "sadasd" },
|
||||
{ "RequestMethod", req.Properties["httpMethod"] },
|
||||
{ "RequestId", req.Id },
|
||||
{ "ItemTypeFlag", req.ItemTypeFlag.ToString() },
|
||||
{ "@sp", "12"},
|
||||
{ "@tr", "23344"},
|
||||
{ "@sk","Server" },
|
||||
{ "@st", req.Timestamp.UtcDateTime.Subtract(req.Duration).ToString("o") }
|
||||
};
|
||||
req.Properties.Remove("httpMethod");
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
|
||||
//we should add a property with name { "Application", "<...>" } other the Seq Span is not looking ok
|
||||
foreach (var prop in req.Properties)
|
||||
{
|
||||
seqEvent.Add(prop.Key, prop.Value);
|
||||
}
|
||||
public async Task LogAsync(RequestTelemetry req, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public async Task LogAsync(Microsoft.ApplicationInsights.Extensibility.IOperationHolder<RequestTelemetry> operationHolder, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var req = operationHolder.Telemetry;
|
||||
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
private async Task SendToSeqAsync(Dictionary<string, object> seqEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
var content = new StringContent(
|
||||
Newtonsoft.Json.JsonConvert.SerializeObject(seqEvent),
|
||||
Encoding.UTF8,
|
||||
"application/vnd.serilog.clef");
|
||||
//https://docs.datalust.co/v2025.1/docs/posting-raw-events
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
|
||||
var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/ingest/clef")
|
||||
{
|
||||
Content = content
|
||||
};
|
||||
{ "@t", req.Timestamp.UtcDateTime.ToString("o") },
|
||||
{ "@mt",req.Name },
|
||||
{ "@l", req.Success??true ? "Information" : "Error" },
|
||||
{ "@sp", req.Id }, //Span id Unique identifier of a span Yes, if the event is a span
|
||||
{ "@tr", req.Context.Operation.Id}, //Trace id An identifier that groups all spans and logs that are in the same trace Yes, if the event is a span
|
||||
{ "@sk","Server" }, //Span kind Describes the relationship of the span to others in the trace: Client, Server, Internal, Producer, or Consumer
|
||||
{ "@st", req.Timestamp.UtcDateTime.Subtract(req.Duration).ToString("o") }, //Start The start ISO 8601 timestamp of this span Yes, if the event is a span
|
||||
{ "SourceContext", typeof(T).FullName },
|
||||
{ "Url", req.Url },
|
||||
{ "RequestId", req.Id },
|
||||
{ "ItemTypeFlag", req.ItemTypeFlag.ToString() }
|
||||
};
|
||||
|
||||
var result = await _httpClient.SendAsync(requestMessage, cancellationToken);
|
||||
|
||||
result.EnsureSuccessStatusCode();
|
||||
}
|
||||
if (!string.IsNullOrEmpty(req.ResponseCode))
|
||||
{
|
||||
if (int.TryParse(req.ResponseCode, out int statusCode))
|
||||
{
|
||||
if (Enum.IsDefined(typeof(System.Net.HttpStatusCode), statusCode))
|
||||
seqEvent["StatusCode"] = $"{statusCode} {(System.Net.HttpStatusCode)statusCode}";
|
||||
else
|
||||
seqEvent["StatusCode"] = $"{statusCode} Unknown";
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(req.Context.Operation.ParentId))
|
||||
seqEvent["@ps"] = req.Context.Operation.ParentId;
|
||||
|
||||
private string MapSeverityToLevel(SeverityLevel? severity)
|
||||
{
|
||||
return severity switch
|
||||
{
|
||||
SeverityLevel.Verbose => "Verbose",
|
||||
SeverityLevel.Information => "Information",
|
||||
SeverityLevel.Warning => "Warning",
|
||||
SeverityLevel.Error => "Error",
|
||||
SeverityLevel.Critical => "Fatal",
|
||||
_ => "Information"
|
||||
};
|
||||
}
|
||||
private string FormatExceptionForSeq(Exception ex)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
var exceptionCount = 0;
|
||||
if (req.Properties.TryGetValue("httpMethod", out string method))
|
||||
{
|
||||
seqEvent["RequestMethod"] = method;
|
||||
seqEvent["@mt"] = $"{req.Properties["httpMethod"]} {req.Name}";
|
||||
req.Properties.Remove("httpMethod");
|
||||
}
|
||||
|
||||
void FormatSingleException(Exception currentEx, int depth)
|
||||
{
|
||||
if (depth > 0) sb.AppendLine("\n--- Inner Exception ---");
|
||||
foreach (var prop in req.Properties)
|
||||
seqEvent.Add($"prop_{prop.Key}", prop.Value);
|
||||
|
||||
sb.AppendLine($"Exception Type: {currentEx.GetType().FullName}");
|
||||
sb.AppendLine($"Message: {currentEx.Message}");
|
||||
sb.AppendLine($"Source: {currentEx.Source}");
|
||||
sb.AppendLine($"HResult: 0x{currentEx.HResult:X8}");
|
||||
sb.AppendLine("Stack Trace:");
|
||||
sb.AppendLine(currentEx.StackTrace?.Trim());
|
||||
foreach (var prop in req.Context.GlobalProperties)
|
||||
seqEvent.Add($"global_{prop.Key}", prop.Value);
|
||||
|
||||
if (currentEx.Data.Count > 0)
|
||||
{
|
||||
sb.AppendLine("Additional Data:");
|
||||
foreach (var key in currentEx.Data.Keys)
|
||||
{
|
||||
sb.AppendLine($" {key}: {currentEx.Data[key]}");
|
||||
}
|
||||
}
|
||||
}
|
||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||
}
|
||||
private async Task SendToSeqAsync(Dictionary<string, object> seqEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
var content = new StringContent(
|
||||
Newtonsoft.Json.JsonConvert.SerializeObject(seqEvent),
|
||||
Encoding.UTF8,
|
||||
"application/vnd.serilog.clef");
|
||||
|
||||
void RecurseExceptions(Exception currentEx, int depth = 0)
|
||||
{
|
||||
if (currentEx is AggregateException aggEx)
|
||||
{
|
||||
foreach (var inner in aggEx.InnerExceptions)
|
||||
{
|
||||
RecurseExceptions(inner, depth);
|
||||
depth++;
|
||||
}
|
||||
}
|
||||
else if (currentEx.InnerException != null)
|
||||
{
|
||||
RecurseExceptions(currentEx.InnerException, depth + 1);
|
||||
}
|
||||
var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/ingest/clef")
|
||||
{
|
||||
Content = content
|
||||
};
|
||||
|
||||
FormatSingleException(currentEx, depth);
|
||||
exceptionCount++;
|
||||
}
|
||||
var result = await _httpClient.SendAsync(requestMessage, cancellationToken);
|
||||
|
||||
RecurseExceptions(ex);
|
||||
sb.Insert(0, $"EXCEPTION CHAIN ({exceptionCount} exceptions):\n");
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
result.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private string MapSeverityToLevel(SeverityLevel? severity)
|
||||
{
|
||||
return severity switch
|
||||
{
|
||||
SeverityLevel.Verbose => "Verbose",
|
||||
SeverityLevel.Information => "Information",
|
||||
SeverityLevel.Warning => "Warning",
|
||||
SeverityLevel.Error => "Error",
|
||||
SeverityLevel.Critical => "Fatal",
|
||||
_ => "Information"
|
||||
};
|
||||
}
|
||||
private string FormatExceptionForSeq(Exception ex)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
var exceptionCount = 0;
|
||||
|
||||
void FormatSingleException(Exception currentEx, int depth)
|
||||
{
|
||||
if (depth > 0) sb.AppendLine("\n--- Inner Exception ---");
|
||||
|
||||
sb.AppendLine($"Exception Type: {currentEx.GetType().FullName}");
|
||||
sb.AppendLine($"Message: {currentEx.Message}");
|
||||
sb.AppendLine($"Source: {currentEx.Source}");
|
||||
sb.AppendLine($"HResult: 0x{currentEx.HResult:X8}");
|
||||
sb.AppendLine("Stack Trace:");
|
||||
sb.AppendLine(currentEx.StackTrace?.Trim());
|
||||
|
||||
if (currentEx.Data.Count > 0)
|
||||
{
|
||||
sb.AppendLine("Additional Data:");
|
||||
foreach (var key in currentEx.Data.Keys)
|
||||
{
|
||||
sb.AppendLine($" {key}: {currentEx.Data[key]}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RecurseExceptions(Exception currentEx, int depth = 0)
|
||||
{
|
||||
if (currentEx is AggregateException aggEx)
|
||||
{
|
||||
foreach (var inner in aggEx.InnerExceptions)
|
||||
{
|
||||
RecurseExceptions(inner, depth);
|
||||
depth++;
|
||||
}
|
||||
}
|
||||
else if (currentEx.InnerException != null)
|
||||
{
|
||||
RecurseExceptions(currentEx.InnerException, depth + 1);
|
||||
}
|
||||
|
||||
FormatSingleException(currentEx, depth);
|
||||
exceptionCount++;
|
||||
}
|
||||
|
||||
RecurseExceptions(ex);
|
||||
sb.Insert(0, $"EXCEPTION CHAIN ({exceptionCount} exceptions):\n");
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
using Autofac;
|
||||
using Core.Logging;
|
||||
using Core.Telemetry;
|
||||
using PlanTempus.Core.Logging;
|
||||
using PlanTempus.Core.Telemetry;
|
||||
|
||||
namespace Core.ModuleRegistry
|
||||
namespace PlanTempus.Core.ModuleRegistry
|
||||
{
|
||||
public class SeqLoggingModule : Module
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,71 +1,48 @@
|
|||
using Autofac;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
|
||||
|
||||
namespace Core.ModuleRegistry
|
||||
namespace PlanTempus.Core.ModuleRegistry
|
||||
{
|
||||
public class TelemetryModule : Module
|
||||
{
|
||||
public TelemetryConfig TelemetryConfig { get; set; }
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
if (TelemetryConfig == null)
|
||||
throw new Exceptions.ConfigurationException("TelemetryConfig is missing");
|
||||
public class TelemetryModule : Module
|
||||
{
|
||||
public TelemetryConfig TelemetryConfig { get; set; }
|
||||
|
||||
//builder.Register(c =>
|
||||
//{
|
||||
// var channel = new Telemetry.DualTelemetryChannel("C:\\logs\\telemetry.log");
|
||||
// channel.DeveloperMode = true;
|
||||
// var config = new TelemetryConfiguration
|
||||
// {
|
||||
|
||||
// ConnectionString = TelemetryConfig.ConnectionString,
|
||||
// TelemetryChannel = channel
|
||||
// };
|
||||
// return new TelemetryClient(config);
|
||||
//}).InstancePerLifetimeScope();
|
||||
|
||||
var telemetryChannel = new InMemoryChannel
|
||||
{
|
||||
DeveloperMode = true
|
||||
};
|
||||
|
||||
//var configuration = new TelemetryConfiguration
|
||||
//{
|
||||
// ConnectionString = TelemetryConfig.ConnectionString,
|
||||
// TelemetryChannel = telemetryChannel
|
||||
//};
|
||||
|
||||
//telemetryChannel.Initialize(configuration);
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
if (TelemetryConfig == null)
|
||||
throw new Exceptions.ConfigurationException("TelemetryConfig is missing");
|
||||
|
||||
|
||||
var tmc = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.CreateDefault();
|
||||
tmc.ConnectionString = TelemetryConfig.ConnectionString;
|
||||
tmc.TelemetryChannel.DeveloperMode = true;
|
||||
var channel = new Telemetry.SeqLoggingTelemetryChannel("C:\\logs\\telemetry.log");
|
||||
var configuration = TelemetryConfiguration.CreateDefault();
|
||||
configuration.ConnectionString = TelemetryConfig.ConnectionString;
|
||||
configuration.TelemetryChannel.DeveloperMode = true;
|
||||
|
||||
tmc.TelemetryChannel = channel;
|
||||
if (TelemetryConfig.UseSeqLoggingTelemetryChannel)
|
||||
configuration.TelemetryChannel = new Telemetry.SeqLoggingTelemetryChannel(); ;
|
||||
|
||||
////var r = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(tmc);
|
||||
////r.Use(next => new Domain.EventTelemetryEnrichers.EnrichWithMetaTelemetry(next));
|
||||
////r.Build();
|
||||
var r = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(configuration);
|
||||
r.Use(next => new Telemetry.Enrichers.EnrichWithMetaTelemetry(next));
|
||||
r.Build();
|
||||
|
||||
//builder.RegisterInstance(configuration);
|
||||
builder.Register(c => new TelemetryClient(tmc)).InstancePerLifetimeScope();
|
||||
//builder.RegisterInstance(configuration);
|
||||
|
||||
//builder.RegisterType<Microsoft.ApplicationInsights.TelemetryClient>()
|
||||
// .InstancePerLifetimeScope();
|
||||
//builder.RegisterType<Microsoft.ApplicationInsights.TelemetryClient>()
|
||||
// .InstancePerLifetimeScope();
|
||||
|
||||
//builder.RegisterType<Microsoft.ApplicationInsights.TelemetryClient>()
|
||||
// .As<Telemetry.ITelemetryClient>()
|
||||
// .InstancePerLifetimeScope();
|
||||
}
|
||||
}
|
||||
var client = new Microsoft.ApplicationInsights.TelemetryClient(configuration);
|
||||
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();
|
||||
|
||||
public class TelemetryConfig
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
}
|
||||
builder.Register(c => client).InstancePerLifetimeScope();
|
||||
}
|
||||
}
|
||||
|
||||
public class TelemetryConfig
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
public bool UseSeqLoggingTelemetryChannel { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
namespace Core.MultiKeyEncryption
|
||||
namespace PlanTempus.Core.MultiKeyEncryption
|
||||
{
|
||||
internal class MasterKey
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ using System.Security.Cryptography;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core.MultiKeyEncryption
|
||||
namespace PlanTempus.Core.MultiKeyEncryption
|
||||
{
|
||||
public class SecureConnectionString
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
22
Core/Telemetry/Enrichers/EnrichWithMetaTelemetry.cs
Normal file
22
Core/Telemetry/Enrichers/EnrichWithMetaTelemetry.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
|
||||
namespace PlanTempus.Core.Telemetry.Enrichers
|
||||
{
|
||||
|
||||
public class EnrichWithMetaTelemetry : ITelemetryProcessor
|
||||
{
|
||||
private readonly ITelemetryProcessor _next;
|
||||
|
||||
public EnrichWithMetaTelemetry(ITelemetryProcessor next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public void Process(ITelemetry item)
|
||||
{
|
||||
//nothing going on here yet :)
|
||||
_next.Process(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
using System.Threading.Channels;
|
||||
namespace Core.Telemetry
|
||||
namespace PlanTempus.Core.Telemetry
|
||||
{
|
||||
public interface IMessageChannel<T> : IDisposable
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
using Microsoft.ApplicationInsights.Channel;
|
||||
using System.Threading.Channels;
|
||||
|
||||
namespace Core.Telemetry
|
||||
namespace PlanTempus.Core.Telemetry
|
||||
{
|
||||
public class MessageChannel : IMessageChannel<ITelemetry>
|
||||
{
|
||||
private readonly Channel<ITelemetry> _channel;
|
||||
public class MessageChannel : IMessageChannel<ITelemetry>
|
||||
{
|
||||
private readonly Channel<ITelemetry> _channel;
|
||||
|
||||
public MessageChannel()
|
||||
{
|
||||
_channel = Channel.CreateUnbounded<ITelemetry>();
|
||||
}
|
||||
public MessageChannel()
|
||||
{
|
||||
_channel = Channel.CreateUnbounded<ITelemetry>();
|
||||
}
|
||||
|
||||
public ChannelWriter<ITelemetry> Writer => _channel.Writer;
|
||||
public ChannelReader<ITelemetry> Reader => _channel.Reader;
|
||||
public ChannelWriter<ITelemetry> Writer => _channel.Writer;
|
||||
public ChannelReader<ITelemetry> Reader => _channel.Reader;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_channel.Writer.Complete();
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_channel.Writer.Complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
using Microsoft.ApplicationInsights.Channel;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace Core.Telemetry
|
||||
namespace PlanTempus.Core.Telemetry
|
||||
{
|
||||
public class SeqLoggingTelemetryChannel : InMemoryChannel, ITelemetryChannel
|
||||
{
|
||||
private readonly string _filePath;
|
||||
public ITelemetryChannel _defaultChannel;
|
||||
static HttpClient _client = new HttpClient();
|
||||
|
||||
|
|
@ -22,9 +21,8 @@ namespace Core.Telemetry
|
|||
}
|
||||
|
||||
|
||||
public SeqLoggingTelemetryChannel(string filePath)
|
||||
public SeqLoggingTelemetryChannel()
|
||||
{
|
||||
_filePath = filePath;
|
||||
}
|
||||
public new void Send(ITelemetry telemetry)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue