Refactoring SetupConsole with DBFactory
This commit is contained in:
parent
8dd01d291d
commit
78d49a9829
20 changed files with 337 additions and 407 deletions
|
|
@ -1,6 +1,3 @@
|
|||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using System.Text;
|
||||
|
||||
namespace PlanTempus.Core.Logging
|
||||
{
|
||||
public record SeqConfiguration(string IngestionEndpoint, string ApiKey, string Environment);
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ namespace PlanTempus.Core.ModuleRegistry
|
|||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
|
||||
builder.RegisterType<MessageChannel>()
|
||||
.As<IMessageChannel<Microsoft.ApplicationInsights.Channel.ITelemetry>>()
|
||||
.SingleInstance();
|
||||
//builder.RegisterType<MessageChannel>()
|
||||
// .As<IMessageChannel<Microsoft.ApplicationInsights.Channel.ITelemetry>>()
|
||||
// .SingleInstance();
|
||||
|
||||
builder.RegisterType<SeqBackgroundService>()
|
||||
.As<Microsoft.Extensions.Hosting.IHostedService>()
|
||||
|
|
|
|||
|
|
@ -6,29 +6,30 @@ namespace PlanTempus.Core.ModuleRegistry
|
|||
{
|
||||
public class TelemetryModule : Module
|
||||
{
|
||||
public TelemetryConfig TelemetryConfig { get; set; }
|
||||
public required TelemetryConfig TelemetryConfig { get; set; }
|
||||
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
if (TelemetryConfig == null)
|
||||
throw new Exceptions.ConfigurationException("TelemetryConfig is missing");
|
||||
|
||||
|
||||
var configuration = TelemetryConfiguration.CreateDefault();
|
||||
configuration.ConnectionString = TelemetryConfig.ConnectionString;
|
||||
configuration.TelemetryChannel.DeveloperMode = true;
|
||||
|
||||
if (TelemetryConfig.UseSeqLoggingTelemetryChannel)
|
||||
configuration.TelemetryChannel = new Telemetry.SeqLoggingTelemetryChannel(); ;
|
||||
{
|
||||
var messageChannel = new Telemetry.MessageChannel();
|
||||
|
||||
var r = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(configuration);
|
||||
r.Use(next => new Telemetry.Enrichers.EnrichWithMetaTelemetry(next));
|
||||
r.Build();
|
||||
builder.RegisterInstance(messageChannel)
|
||||
.As<Telemetry.IMessageChannel<ITelemetry>>()
|
||||
.SingleInstance();
|
||||
|
||||
//builder.RegisterInstance(configuration);
|
||||
configuration.TelemetryChannel = new Telemetry.SeqLoggingTelemetryChannel(messageChannel);
|
||||
}
|
||||
|
||||
var telemetryProcessorChain = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(configuration);
|
||||
telemetryProcessorChain.Use(next => new Telemetry.Enrichers.EnrichWithMetaTelemetry(next));
|
||||
telemetryProcessorChain.Build();
|
||||
|
||||
//builder.RegisterType<Microsoft.ApplicationInsights.TelemetryClient>()
|
||||
// .InstancePerLifetimeScope();
|
||||
|
||||
var client = new Microsoft.ApplicationInsights.TelemetryClient(configuration);
|
||||
client.Context.GlobalProperties["Application"] = GetType().Namespace.Split('.')[0];
|
||||
|
|
|
|||
|
|
@ -1,50 +1,27 @@
|
|||
using Microsoft.ApplicationInsights.Channel;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace PlanTempus.Core.Telemetry
|
||||
{
|
||||
public class SeqLoggingTelemetryChannel : InMemoryChannel, ITelemetryChannel
|
||||
{
|
||||
public ITelemetryChannel _defaultChannel;
|
||||
static HttpClient _client = new HttpClient();
|
||||
private readonly IMessageChannel<ITelemetry> _messageChannel;
|
||||
|
||||
static SeqLoggingTelemetryChannel()
|
||||
{
|
||||
_client = new HttpClient()
|
||||
{
|
||||
BaseAddress = new Uri("http://localhost:5341"),
|
||||
Timeout = TimeSpan.FromSeconds(30)
|
||||
};
|
||||
|
||||
_client.DefaultRequestHeaders.Accept.Clear();
|
||||
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
}
|
||||
|
||||
|
||||
public SeqLoggingTelemetryChannel()
|
||||
public SeqLoggingTelemetryChannel(IMessageChannel<ITelemetry> messageChannel)
|
||||
{
|
||||
_messageChannel = messageChannel;
|
||||
}
|
||||
public new void Send(ITelemetry telemetry)
|
||||
{
|
||||
//var l = new SeqLogger(_client, "", "");
|
||||
|
||||
//l.LogToSeq(
|
||||
// "Bruger {UserId} loggede ind",
|
||||
// "Debug",
|
||||
// new Dictionary<string, object> { { "UserId", "12345" }, { "Counter", "i++" } }
|
||||
// );
|
||||
|
||||
|
||||
//if (telemetry is Microsoft.ApplicationInsights.DataContracts.TraceTelemetry trace)
|
||||
//{
|
||||
// var severity = trace.SeverityLevel;
|
||||
// Console.WriteLine($"Trace severity: {severity}, Message: {trace.Message}");
|
||||
//}
|
||||
|
||||
var writeTask = _messageChannel.Writer.WriteAsync(telemetry).AsTask();
|
||||
writeTask.ContinueWith(t =>
|
||||
{
|
||||
if (t.Exception != null)
|
||||
{
|
||||
throw t.Exception;
|
||||
}
|
||||
}, TaskContinuationOptions.OnlyOnFaulted);
|
||||
|
||||
base.Send(telemetry);
|
||||
var logEntry = $"{DateTime.UtcNow:u}|{telemetry.Context.Operation.Name}|{telemetry.Context.Operation.Id}";
|
||||
//File.AppendAllText(_filePath, logEntry + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue