feat: API-Payload-Logging in data/logs/ für Debugging
- LogPayload() schreibt den kompletten Claude-API-Request als indented JSON - Dateiname: <unixtime>-<sessionId>-<chainId>-iter<N>-payload.json - Beide Pfade abgedeckt: StreamChat (Web) + ChatNonStreaming (Telegram/Cronjobs) - try/catch: Logging-Fehler crashen nie die App
This commit is contained in:
1 parent
8fc29c82d9
commit
d482f5dca3
1 file changed
+17
-2
@@ -94,7 +94,8 @@ public class AnthropicService
|
||||
}
|
||||
|
||||
var client = CreateClient();
|
||||
var json = JsonSerializer.Serialize(payload);
|
||||
var json = JsonSerializer.Serialize(payload, new JsonSerializerOptions { WriteIndented = true });
|
||||
LogPayload(json, sessionId, chainId, iterations);
|
||||
var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var httpResp = await client.SendAsync(
|
||||
@@ -292,7 +293,8 @@ public class AnthropicService
|
||||
}
|
||||
|
||||
var client = CreateClient();
|
||||
var json = JsonSerializer.Serialize(payload);
|
||||
var json = JsonSerializer.Serialize(payload, new JsonSerializerOptions { WriteIndented = true });
|
||||
LogPayload(json, ctx.SessionId, ctx.ChainId, iterations);
|
||||
var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var httpResp = await client.SendAsync(
|
||||
@@ -414,6 +416,19 @@ public class AnthropicService
|
||||
return finalText.Length > 0 ? finalText.ToString() : "(Keine Textantwort)";
|
||||
}
|
||||
|
||||
private static void LogPayload(string json, string sessionId, string chainId, int iteration)
|
||||
{
|
||||
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);
|
||||
}
|
||||
catch { /* Logging darf nie die App crashen */ }
|
||||
}
|
||||
|
||||
private async Task FinalizeChain(ChainRecord chain, TokenUsage usage, int iterations, string stopReason, System.Diagnostics.Stopwatch sw, string streamErrorInfo = null)
|
||||
{
|
||||
chain.InputTokens = usage.InputTokens;
|
||||
|
||||
Reference in new issue
Block a user