Auto stash before merge of "main" and "origin/main"

This commit is contained in:
Janus C. H. Knudsen 2025-02-06 17:48:24 +01:00
parent 72544d62e2
commit 5ca874abe9
6 changed files with 81 additions and 14 deletions

View file

@ -14,10 +14,7 @@
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" /> <PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.22.0" /> <PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.22.0" />
<PackageReference Include="npgsql" Version="9.0.2" /> <PackageReference Include="npgsql" Version="9.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" /> <PackageReference Include="Seq.Api" Version="2024.3.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="9.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

51
Core/Telemetry/Class1.cs Normal file
View file

@ -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<string, object> properties)
{
var seqEvent = new Dictionary<string, object>
{
{ "@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();
}
}
}

View file

@ -11,10 +11,17 @@ namespace Core.Telemetry
{ {
_filePath = filePath; _filePath = filePath;
} }
public new void Send(ITelemetry item) public new void Send(ITelemetry telemetry)
{ {
base.Send(item); if (telemetry is Microsoft.ApplicationInsights.DataContracts.TraceTelemetry trace)
var logEntry = $"{DateTime.UtcNow:u}|{item.Context.Operation.Name}|{item.Context.Operation.Id}"; {
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); //File.AppendAllText(_filePath, logEntry + Environment.NewLine);
} }
} }

View file

@ -1,4 +1,4 @@
using Autofac; using Autofac;
namespace SetupInfrastructure namespace SetupInfrastructure
{ {

View file

@ -21,17 +21,31 @@ namespace Tests
conn.ExecuteSql("SELECT 1 as p"); conn.ExecuteSql("SELECT 1 as p");
} }
static HttpClient _client = new HttpClient();
[TestMethod] [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<string, object> { { "UserId", "12345" }, { "Counter", i++ } }
);
}
var logger = Container.Resolve<Microsoft.ApplicationInsights.TelemetryClient>(); var logger = Container.Resolve<Microsoft.ApplicationInsights.TelemetryClient>();
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
logger.TrackTrace("Hello 23"); logger.TrackTrace("Hello 23" , Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
} }
} }
[TestMethod] [TestMethod]

View file

@ -6,8 +6,6 @@ using Core.ModuleRegistry;
using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Core.Configurations.JsonConfigProvider; using Core.Configurations.JsonConfigProvider;
using Serilog;
using Serilog.Extensions.Logging;
namespace Tests namespace Tests
{ {
/// <summary> /// <summary>