feat: payload logging (on/off + gzip) via appsettings + fix destroy messages ref

This commit is contained in:
Meleki committed 2026-03-04 16:10:09 +01:00
1 parent d482f5dca3
commit f552b44b05
4 files changed
+29 -7

No files matched your search

+4 -1
View File
@@ -35,10 +35,13 @@ builder.Services.AddSingleton(chatService);
// AnthropicService (Singleton – shared zwischen ChatEndpoint und TelegramService)
var anthropicToken = builder.Configuration["AnthropicToken"] ?? "";
var payloadLogging = builder.Configuration.GetValue<bool>("PayloadLogging", false);
var payloadLoggingGzip = builder.Configuration.GetValue<bool>("PayloadLoggingGzip", false);
builder.Services.AddSingleton<AnthropicService>(sp =>
new AnthropicService(
sp.GetRequiredService<IHttpClientFactory>(),
anthropicToken, toolService, dbService, memoryService));
anthropicToken, toolService, dbService, memoryService,
payloadLogging, payloadLoggingGzip));
// Telegram Bot (pro Session – Token aus Session-Config)
var maxIterations = builder.Configuration.GetValue<int>("MaxIterations", 100);
+22 -4
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
@@ -19,14 +20,18 @@ public class AnthropicService
private readonly ToolService _toolService;
private readonly DatabaseService _db;
private readonly MemoryService _memoryService;
private readonly bool _payloadLogging;
private readonly bool _payloadLoggingGzip;
public AnthropicService(IHttpClientFactory httpFactory, string token, ToolService toolService, DatabaseService db, MemoryService memoryService = null)
public AnthropicService(IHttpClientFactory httpFactory, string token, ToolService toolService, DatabaseService db, MemoryService memoryService = null, bool payloadLogging = false, bool payloadLoggingGzip = false)
{
_httpFactory = httpFactory;
_token = token;
_toolService = toolService;
_db = db;
_memoryService = memoryService;
_payloadLogging = payloadLogging;
_payloadLoggingGzip = payloadLoggingGzip;
}
public async Task StreamChat(
@@ -416,15 +421,28 @@ public class AnthropicService
return finalText.Length > 0 ? finalText.ToString() : "(Keine Textantwort)";
}
private static void LogPayload(string json, string sessionId, string chainId, int iteration)
private void LogPayload(string json, string sessionId, string chainId, int iteration)
{
if (!_payloadLogging) return;
try
{
var logsDir = Path.Combine(AppContext.BaseDirectory, "data", "logs");
Directory.CreateDirectory(logsDir);
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var fileName = $"{timestamp}-{sessionId}-{chainId}-iter{iteration}-payload.json";
File.WriteAllText(Path.Combine(logsDir, fileName), json);
var baseName = $"{timestamp}-{sessionId}-{chainId}-iter{iteration}-payload";
if (_payloadLoggingGzip)
{
var filePath = Path.Combine(logsDir, baseName + ".json.gz");
using var fs = File.Create(filePath);
using var gz = new GZipStream(fs, CompressionLevel.Fastest);
using var writer = new StreamWriter(gz, Encoding.UTF8);
writer.Write(json);
}
else
{
File.WriteAllText(Path.Combine(logsDir, baseName + ".json"), json);
}
}
catch { /* Logging darf nie die App crashen */ }
}
+3 -1
View File
@@ -7,5 +7,7 @@
},
"AllowedHosts": "*",
"AnthropicToken": "",
"MaxIterations": 100
"MaxIterations": 100,
"PayloadLogging": false,
"PayloadLoggingGzip": false
}
-1
View File
@@ -384,7 +384,6 @@ registerPage('chat', {
_streamingPollInterval = null;
}
stopHistoryPolling();
messages.length = 0;
lastKnownMessageId = 0;
loadedArchiveCount = 0;
isStreaming = false;