feat: payload logging (on/off + gzip) via appsettings + fix destroy messages ref
This commit is contained in:
1 parent
d482f5dca3
commit
f552b44b05
4 files changed
+29
-7
No files matched your search
@@ -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 */ }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user