diff --git a/Core/Core.csproj b/Core/Core.csproj index 437e6dd..dd31f27 100644 --- a/Core/Core.csproj +++ b/Core/Core.csproj @@ -14,10 +14,7 @@ - - - - + diff --git a/Core/Telemetry/Class1.cs b/Core/Telemetry/Class1.cs new file mode 100644 index 0000000..4792b38 --- /dev/null +++ b/Core/Telemetry/Class1.cs @@ -0,0 +1,51 @@ +using Seq.Api; +using Seq.Api.Client; +using Seq.Api.Model.Events; +using Seq.Api.Model.LogEvents; +using Seq.Api.Model.Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace Core.Telemetry +{ + public class SeqLogger + { + private readonly string _seqUrl; + private readonly string _apiKey; // Optional + HttpClient _httpClient; + public SeqLogger(HttpClient httpClient, string seqUrl, string apiKey = null) + { + _seqUrl = seqUrl; + _apiKey = apiKey; + _httpClient = httpClient; + } + + public async Task LogToSeq(string message, string level, Dictionary properties) + { + + var seqEvent = new Dictionary + { + { "@t", DateTime.UtcNow.ToString("o") }, + { "@mt", message }, + { "@l", level } // "Information", "Warning", "Error", etc. + }; + + foreach (var prop in properties) + { + seqEvent.Add(prop.Key, prop.Value); + } + + var content = new StringContent( + JsonSerializer.Serialize(seqEvent), + Encoding.UTF8, + "application/vnd.serilog.clef"); + + var response = await _httpClient.PostAsync("/ingest/clef?apiKey=Gt8hS9ClGNfOCAdswDlW", content); + response.EnsureSuccessStatusCode(); + } + } +} diff --git a/Core/Telemetry/DebugTelemetryChannel.cs b/Core/Telemetry/DebugTelemetryChannel.cs index 43ae72f..1a0b53f 100644 --- a/Core/Telemetry/DebugTelemetryChannel.cs +++ b/Core/Telemetry/DebugTelemetryChannel.cs @@ -11,10 +11,17 @@ namespace Core.Telemetry { _filePath = filePath; } - public new void Send(ITelemetry item) + public new void Send(ITelemetry telemetry) { - base.Send(item); - var logEntry = $"{DateTime.UtcNow:u}|{item.Context.Operation.Name}|{item.Context.Operation.Id}"; + if (telemetry is Microsoft.ApplicationInsights.DataContracts.TraceTelemetry trace) + { + var severity = trace.SeverityLevel; + Console.WriteLine($"Trace severity: {severity}, Message: {trace.Message}"); + } + + + base.Send(telemetry); + var logEntry = $"{DateTime.UtcNow:u}|{telemetry.Context.Operation.Name}|{telemetry.Context.Operation.Id}"; //File.AppendAllText(_filePath, logEntry + Environment.NewLine); } } diff --git a/SetupInfrastructure/Program.cs b/SetupInfrastructure/Program.cs index 7df378f..2e19f6b 100644 --- a/SetupInfrastructure/Program.cs +++ b/SetupInfrastructure/Program.cs @@ -1,4 +1,4 @@ -using Autofac; +using Autofac; namespace SetupInfrastructure { diff --git a/Tests/PostgresTests.cs b/Tests/PostgresTests.cs index ab55e33..1240242 100644 --- a/Tests/PostgresTests.cs +++ b/Tests/PostgresTests.cs @@ -21,17 +21,31 @@ namespace Tests conn.ExecuteSql("SELECT 1 as p"); } + static HttpClient _client = new HttpClient(); [TestMethod] - public void MyTestMethod() + public async Task MyTestMethod() { + _client.BaseAddress = new Uri("http://localhost:5341"); + + var l = new SeqLogger(_client, "", ""); + + for (int i = 0; i < 20; i++) + { + + await l.LogToSeq( + "Bruger {UserId} loggede ind", + "Debug", + new Dictionary { { "UserId", "12345" }, { "Counter", i++ } } + ); + } var logger = Container.Resolve(); for (int i = 0; i < 5; i++) { - logger.TrackTrace("Hello 23"); - + logger.TrackTrace("Hello 23" , Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information); + } - + } [TestMethod] diff --git a/Tests/TestFixture.cs b/Tests/TestFixture.cs index 85ae847..2aa52f8 100644 --- a/Tests/TestFixture.cs +++ b/Tests/TestFixture.cs @@ -6,8 +6,6 @@ using Core.ModuleRegistry; using Microsoft.ApplicationInsights; using Microsoft.Extensions.Logging; using Core.Configurations.JsonConfigProvider; -using Serilog; -using Serilog.Extensions.Logging; namespace Tests { ///