summaryrefslogtreecommitdiff
path: root/src/Agent.App/Logging
diff options
context:
space:
mode:
Diffstat (limited to 'src/Agent.App/Logging')
-rw-r--r--src/Agent.App/Logging/JsonLinesRuntimeObserver.cs37
-rw-r--r--src/Agent.App/Logging/RollingFileLog.cs55
2 files changed, 92 insertions, 0 deletions
diff --git a/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs b/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs
new file mode 100644
index 0000000..b7cbd26
--- /dev/null
+++ b/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs
@@ -0,0 +1,37 @@
+using System.Text.Json;
+using Agent.Core.Ollama;
+using Agent.Core.Runtime;
+using Agent.Core.Tools;
+
+namespace Agent.App.Logging;
+
+public sealed class JsonLinesRuntimeObserver(string logDirectory) : IAgentRuntimeObserver
+{
+ private readonly object _sync = new();
+ private readonly string _path = Path.Combine(logDirectory, "llm.jsonl");
+
+ public void LlmRequest(Guid taskId, int iteration, IReadOnlyList<OllamaMessage> messages) => Write(new
+ {
+ timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.request", iteration,
+ messages = messages.Select(static message => new { message.Role, message.Content, image_count = message.Images?.Count ?? 0, message.ToolName })
+ });
+
+ public void LlmResponse(Guid taskId, int iteration, OllamaResult result) => Write(new
+ {
+ timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.response", iteration,
+ result.Content, input_tokens = result.InputTokens, output_tokens = result.OutputTokens,
+ tool_calls = result.ToolCalls?.Select(static call => call.Function.Name).ToArray() ?? []
+ });
+
+ public void ToolResult(Guid taskId, string toolName, ToolExecutionResult result) => Write(new
+ {
+ timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.tool_result", tool_name = toolName,
+ result.Success, result.ExternalEffect, result.Output, result.FailureFingerprint
+ });
+
+ private void Write(object value)
+ {
+ string line = JsonSerializer.Serialize(value);
+ lock (_sync) File.AppendAllText(_path, line + Environment.NewLine);
+ }
+}
diff --git a/src/Agent.App/Logging/RollingFileLog.cs b/src/Agent.App/Logging/RollingFileLog.cs
new file mode 100644
index 0000000..801768d
--- /dev/null
+++ b/src/Agent.App/Logging/RollingFileLog.cs
@@ -0,0 +1,55 @@
+using System.Globalization;
+
+namespace Agent.App.Logging;
+
+public sealed class RollingFileLog : IDisposable
+{
+ private readonly object _sync = new();
+ private readonly string _path;
+ private readonly long _maxBytes;
+ private readonly int _retainedFiles;
+ private StreamWriter _writer;
+
+ public RollingFileLog(string directory, long maxBytes, int retainedFiles)
+ {
+ Directory.CreateDirectory(directory);
+ _path = Path.Combine(directory, "application.log");
+ _maxBytes = maxBytes;
+ _retainedFiles = retainedFiles;
+ _writer = Open();
+ }
+
+ public void Information(string message) => Write("INF", message);
+ public void Error(string message) => Write("ERR", message);
+
+ private void Write(string level, string message)
+ {
+ lock (_sync)
+ {
+ if (_writer.BaseStream.Length >= _maxBytes) Rotate();
+ string line = string.Create(CultureInfo.InvariantCulture, $"{DateTimeOffset.UtcNow:O} [{level}] {message}");
+ Console.Error.WriteLine(line);
+ _writer.WriteLine(line);
+ _writer.Flush();
+ }
+ }
+
+ private StreamWriter Open() => new(new FileStream(_path, FileMode.Append, FileAccess.Write, FileShare.Read), new System.Text.UTF8Encoding(false));
+
+ private void Rotate()
+ {
+ _writer.Dispose();
+ for (int index = _retainedFiles - 1; index >= 1; index--)
+ {
+ string source = $"{_path}.{index}";
+ if (File.Exists(source)) File.Move(source, $"{_path}.{index + 1}", true);
+ }
+ if (File.Exists(_path)) File.Move(_path, $"{_path}.1", true);
+ _writer = Open();
+ }
+
+ public void Dispose()
+ {
+ lock (_sync) _writer.Dispose();
+ }
+}