36 lines
1.0 KiB
C#
36 lines
1.0 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace TermRemoteCtl.Agent.History;
|
|
|
|
public sealed class SessionIoJournalStore
|
|
{
|
|
private static readonly JsonSerializerOptions SerializerOptions = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
};
|
|
|
|
private readonly string _sessionRoot;
|
|
|
|
public SessionIoJournalStore(string rootPath)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(rootPath);
|
|
|
|
_sessionRoot = Path.Combine(rootPath, "sessions");
|
|
Directory.CreateDirectory(_sessionRoot);
|
|
}
|
|
|
|
public async Task AppendAsync(SessionIoEvent ioEvent, CancellationToken cancellationToken)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(ioEvent);
|
|
|
|
var filePath = Path.Combine(_sessionRoot, $"{ioEvent.SessionId}.io.jsonl");
|
|
var line = JsonSerializer.Serialize(ioEvent, SerializerOptions) + Environment.NewLine;
|
|
await File.AppendAllTextAsync(
|
|
filePath,
|
|
line,
|
|
new UTF8Encoding(false),
|
|
cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|