From b2c0919a8c952d755bcf18c6157c41f09c79589b Mon Sep 17 00:00:00 2001 From: "Janus C. H. Knudsen" Date: Wed, 5 Feb 2025 18:38:29 +0100 Subject: [PATCH] Adds a DebugChannel for TelemetryClient --- Application/Startup.cs | 1 + Core/Core.csproj | 5 ++ Core/ModuleRegistry/TelemetryModule.cs | 83 ++++++++++++++++++------- Core/Telemetry/DebugTelemetryChannel.cs | 21 +++++++ SetupInfrastructure/Program.cs | 66 ++++++++++++++------ Tests/PostgresTests.cs | 16 ++++- Tests/TestFixture.cs | 31 +++++++-- Tests/appconfiguration.dev.json | 27 ++++++++ 8 files changed, 203 insertions(+), 47 deletions(-) create mode 100644 Core/Telemetry/DebugTelemetryChannel.cs diff --git a/Application/Startup.cs b/Application/Startup.cs index 757db74..d64ad4b 100644 --- a/Application/Startup.cs +++ b/Application/Startup.cs @@ -1,6 +1,7 @@ using Autofac; using Core.Configurations.JsonConfigProvider; using Core.Configurations; + namespace PlanTempus { public class Startup diff --git a/Core/Core.csproj b/Core/Core.csproj index cec383b..437e6dd 100644 --- a/Core/Core.csproj +++ b/Core/Core.csproj @@ -12,7 +12,12 @@ + + + + + diff --git a/Core/ModuleRegistry/TelemetryModule.cs b/Core/ModuleRegistry/TelemetryModule.cs index 0983ca4..db5c25b 100644 --- a/Core/ModuleRegistry/TelemetryModule.cs +++ b/Core/ModuleRegistry/TelemetryModule.cs @@ -1,30 +1,71 @@ using Autofac; +using Microsoft.ApplicationInsights; +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; namespace 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; } + 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; + //builder.Register(c => + //{ + // var channel = new Telemetry.DualTelemetryChannel("C:\\logs\\telemetry.log"); + // channel.DeveloperMode = true; + // var config = new TelemetryConfiguration + // { - //var r = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(tmc); - //r.Use(next => new Domain.EventTelemetryEnrichers.EnrichWithMetaTelemetry(next)); - //r.Build(); + // ConnectionString = TelemetryConfig.ConnectionString, + // TelemetryChannel = channel + // }; + // return new TelemetryClient(config); + //}).InstancePerLifetimeScope(); - builder.RegisterInstance(tmc); - builder.RegisterType().InstancePerLifetimeScope(); - } - } + var telemetryChannel = new InMemoryChannel + { + DeveloperMode = true + }; - public class TelemetryConfig - { - public string ConnectionString { get; set; } - } + //var configuration = new TelemetryConfiguration + //{ + // ConnectionString = TelemetryConfig.ConnectionString, + // TelemetryChannel = telemetryChannel + //}; + + //telemetryChannel.Initialize(configuration); + + + var tmc = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.CreateDefault(); + tmc.ConnectionString = TelemetryConfig.ConnectionString; + tmc.TelemetryChannel.DeveloperMode = true; + var channel = new Telemetry.DebugTelemetryChannel("C:\\logs\\telemetry.log"); + + tmc.TelemetryChannel = channel; + + ////var r = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(tmc); + ////r.Use(next => new Domain.EventTelemetryEnrichers.EnrichWithMetaTelemetry(next)); + ////r.Build(); + + //builder.RegisterInstance(configuration); + builder.Register(c => new TelemetryClient(tmc)).InstancePerLifetimeScope(); + + //builder.RegisterType() + // .InstancePerLifetimeScope(); + + //builder.RegisterType() + // .As() + // .InstancePerLifetimeScope(); + } + } + + public class TelemetryConfig + { + public string ConnectionString { get; set; } + } } diff --git a/Core/Telemetry/DebugTelemetryChannel.cs b/Core/Telemetry/DebugTelemetryChannel.cs new file mode 100644 index 0000000..43ae72f --- /dev/null +++ b/Core/Telemetry/DebugTelemetryChannel.cs @@ -0,0 +1,21 @@ +using Microsoft.ApplicationInsights.Channel; + +namespace Core.Telemetry +{ + public class DebugTelemetryChannel : InMemoryChannel, ITelemetryChannel + { + private readonly string _filePath; + public ITelemetryChannel _defaultChannel; + + public DebugTelemetryChannel(string filePath) + { + _filePath = filePath; + } + public new void Send(ITelemetry item) + { + base.Send(item); + var logEntry = $"{DateTime.UtcNow:u}|{item.Context.Operation.Name}|{item.Context.Operation.Id}"; + //File.AppendAllText(_filePath, logEntry + Environment.NewLine); + } + } +} diff --git a/SetupInfrastructure/Program.cs b/SetupInfrastructure/Program.cs index 0934dda..420fc3b 100644 --- a/SetupInfrastructure/Program.cs +++ b/SetupInfrastructure/Program.cs @@ -1,22 +1,48 @@ -namespace SetupInfrastructure +using Microsoft.ApplicationInsights; +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; + +namespace SetupInfrastructure { - /// - /// SETUP APPLICATION USER NAMED sathumper - /// - /// This should be handled on the Postgresql db server with a superadmin or similar. - /// - /// Execute SQL CreateRole.txt - /// - /// After that is executed it is time for running this main program - /// Remember to use the newly created sathumper - /// "ConnectionStrings": { - /// "ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=;" - /// - internal class Program - { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } - } + /// + /// SETUP APPLICATION USER NAMED sathumper + /// + /// This should be handled on the Postgresql db server with a superadmin or similar. + /// + /// Execute SQL CreateRole.txt + /// + /// After that is executed it is time for running this main program + /// Remember to use the newly created sathumper + /// "ConnectionStrings": { + /// "ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=;" + /// + internal class Program + { + static async Task Main(string[] args) + { + + var telemetryChannel = new ServerTelemetryChannel(); + var configuration = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.CreateDefault(); + configuration.ConnectionString = "InstrumentationKey=2d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/"; + configuration.TelemetryChannel = telemetryChannel; + + + + telemetryChannel.Initialize(configuration); + + telemetryChannel.Send(item); + + + var log = new TelemetryClient(configuration); + log.TrackTrace("Console log med kanal 2"); + + log.Flush(); + + Console.WriteLine("Hello, World!"); + await Task.Delay(5000); + + Console.Read(); + } + } } diff --git a/Tests/PostgresTests.cs b/Tests/PostgresTests.cs index da4e9d7..ab55e33 100644 --- a/Tests/PostgresTests.cs +++ b/Tests/PostgresTests.cs @@ -1,6 +1,9 @@ using Autofac; using System.Data; -using Insight.Database; +using Insight.Database; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.Extensions.Logging; +using Core.Telemetry; namespace Tests { @@ -18,7 +21,18 @@ namespace Tests conn.ExecuteSql("SELECT 1 as p"); } + [TestMethod] + public void MyTestMethod() + { + var logger = Container.Resolve(); + for (int i = 0; i < 5; i++) + { + logger.TrackTrace("Hello 23"); + + } + + } [TestMethod] public void TryTenantSetupService() diff --git a/Tests/TestFixture.cs b/Tests/TestFixture.cs index 8b0d0b5..859cf38 100644 --- a/Tests/TestFixture.cs +++ b/Tests/TestFixture.cs @@ -6,6 +6,8 @@ using Core.ModuleRegistry; using Microsoft.ApplicationInsights; using Microsoft.Extensions.Logging; using Core.Configurations.JsonConfigProvider; +using Serilog; +using Serilog.Extensions.Logging; namespace Tests { /// @@ -43,13 +45,31 @@ namespace Tests { IConfigurationRoot configuration = Configuration(); + //var logger = new LoggerConfiguration() + // .MinimumLevel.Verbose() + // .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) + // .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error) + // .WriteTo.Seq("http://localhost:5341", apiKey: "Gt8hS9ClGNfOCAdswDlW") + // .WriteTo.ApplicationInsights(configuration.Get("ApplicationInsights:ConnectionString"), + // TelemetryConverter.Traces) + // .Enrich.FromLogContext() + // .Enrich.WithMachineName() + // .CreateLogger(); + + //Log.Logger = logger; + + //Log.Logger.Verbose("Is thos work"); var builder = new ContainerBuilder(); - builder.RegisterInstance(new LoggerFactory()) - .As(); + + + //builder.Register(c => new SerilogLoggerFactory(logger)) + // .As() + // .SingleInstance(); builder.RegisterGeneric(typeof(Logger<>)) - .As(typeof(ILogger<>)) - .SingleInstance(); + .As(typeof(ILogger<>)) + .SingleInstance(); + builder.RegisterModule(new Core.ModuleRegistry.DbPostgreSqlModule { @@ -62,13 +82,14 @@ namespace Tests }); - ContainerBuilder = builder; } [TestCleanup] public void CleanUp() { + //Serilog.Log.CloseAndFlush(); + Trace.Flush(); var telemetryClient = Container.Resolve(); telemetryClient.Flush(); diff --git a/Tests/appconfiguration.dev.json b/Tests/appconfiguration.dev.json index 6c52fa4..188c29f 100644 --- a/Tests/appconfiguration.dev.json +++ b/Tests/appconfiguration.dev.json @@ -28,5 +28,32 @@ "Min": { "Limit": 9 } } } + }, + "Serilog": { + "MinimumLevel": { + "Default": "Information", + "Override": { + "Microsoft": "Warning", + "System": "Warning" + } + }, + "WriteTo": [ + { + "Name": "Seq", + "Args": { + "serverUrl": "http://localhost:5341", + "apiKey": "" + } + } + ], + "Enrich": [ + "WithMachineName", + "WithThreadId", + "WithProcessId", + "WithEnvironmentName" + ], + "Properties": { + "Application": "PlanTempus" + } } } \ No newline at end of file