---
title: "Log local LLM run events to JSONL in C#"
url: https://recursiveai.org/onellm/cookbook/capture-llm-run-events-as-jsonl/
description: "Log local LLM run events to a JSONL file in C#. Use OneLLM’s record sink, then read back the run, message, and inference records without an API key."
version: "1.0.1"
---

# Log local LLM run events to JSONL in C#

**OneLLM** is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/capture-llm-run-events-as-jsonl/capture-llm-run-events-as-jsonl.zip)

Use `JsonlRecordSink` to save a local run as JSON Lines: one event per line.

```shell
dotnet add package RecursiveAI.OneLlm
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneLlm;
using OneLlm.Records;
using OneLlm.Testing;

var runtime = new Runtime();
var sink = new JsonlRecordSink("records");
runtime.Sinks.Add(sink);
var model = new ScriptedProvider("openai");
model.EnqueueText("Four.");
runtime.Providers.Set("openai", model);
var run = await runtime.AskAsync(new AskRequest
{
    Model = "gpt-6-luna", Prompt = "What is 2 + 2?"
});
Console.WriteLine(run.Text);
```

Unzip [the example](https://recursiveai.org/onellm/cookbook/capture-llm-run-events-as-jsonl/capture-llm-run-events-as-jsonl.zip), then `dotnet run`.

Writes `records/<instanceId>.jsonl`. The prompt and reply are in the code; OneLLM’s scripted provider returns the reply offline, with no key or local model server. The `gpt-6-luna` name selects the scripted route here; the sink doesn’t use it. Prints:

```text
Four.
```

## The full program

The download gets the exact file path from `PathOf`, reads the records back, and prints their kinds in order.

```csharp
using OneLlm;
using OneLlm.Records;
using OneLlm.Testing;

var runtime = new Runtime();
var sink = new JsonlRecordSink("records");
runtime.Sinks.Add(sink);
runtime.Sinks.OnError = (_, error) =>
    Console.Error.WriteLine(error.Message);

var model = new ScriptedProvider("openai");
model.EnqueueText("Four.");
runtime.Providers.Set("openai", model);
var run = await runtime.AskAsync(new AskRequest
{
    Model = "gpt-6-luna", Prompt = "What is 2 + 2?"
});

string path = sink.PathOf(run.Instance.InstanceId);
Console.WriteLine(string.Join(", ",
    RecordReader.ReadFile(path).Select(record => record.Kind)));
```

`dotnet run` prints:

```text
run, message, inference, message, run
```

That’s the start of the run, the prompt, the model call, the reply, and the end of the run. If writing fails (for example, the directory isn’t writable), OneLLM calls `Sinks.OnError` and continues the run; the download sends that error to standard error.

## Variations

**Read the provider and operation from inference records.** Add this after `path` is set in the full program. It reads the existing file; it doesn’t call the model.

```csharp
foreach (var record in RecordReader.ReadFile(path))
    if (record.Kind == "inference")
    {
        Console.WriteLine(
            record.Json.GetProperty("provider").GetString());
        Console.WriteLine(
            record.Json.GetProperty("operation").GetString());
    }
```

```text
openai
chat
```

**Use a local model server instead.** Keep the sink, but replace the scripted provider setup with the Ollama preset registration in [Call Ollama from C#](https://recursiveai.org/onellm/cookbook/call-ollama-from-csharp/#variations) and set `Model` to `ollama:llama3.2` if that model is installed.

## Related recipes

- [Call Ollama from C#](https://recursiveai.org/onellm/cookbook/call-ollama-from-csharp/)
- [Trace LLM calls with OpenTelemetry](https://recursiveai.org/onellm/cookbook/trace-llm-calls-with-opentelemetry/)
- [Test LLM code without an API key](https://recursiveai.org/onellm/cookbook/test-llm-code-without-api-key/)

## The short answer

Add a `JsonlRecordSink` to the OneLLM runtime before you start the run, then use the result’s instance ID to find the file. The sink appends records to that instance’s `.jsonl` file as the run produces them. Changing the model provider doesn’t change where the records go; the sink belongs to the runtime, not to the model server.

## What is the difference between JSON and JSONL format?

A plain JSON file usually represents a single value, such as an array of records. In [JSON Lines](https://jsonlines.org/), each line is a JSON value of its own. OneLLM appends one object at a time without reopening an array or adding commas between objects. That makes a file convenient to inspect even if a run is still producing events. Whitespace inside a value is escaped where needed; don’t pretty-print the objects across several lines if you want to keep the file in JSONL format.

OneLLM uses `kind` to tell you how to interpret a record. A `run` record may mark a start or an end, so `kind` alone doesn’t tell you which one it is. The first line of one example run was:

```json
{"kind":"run","time":"2026-11-20T12:12:12.3865369Z","instanceId":"i-43650ddd87434e41a550debb085560c4","event":"started","state":"running","model":"gpt-6-luna"}
```

Each run generates its own instance ID and timestamps. Records without an instance ID go to `_runtime.jsonl` rather than to a run file.

## How do you read a JSONL file in C#?

`RecordReader.ReadFile` returns the file’s records in order and gives you the parsed JSON payload on each record’s `Json` property. It returns an empty list for a missing file, so if a run completed but you get no records, check the directory and the sink’s error callback. For long files you can read one line at a time with `File.ReadLines` and parse each nonempty line with `RecordReader.ParseLine`:

```csharp
foreach (var line in File.ReadLines(path))
    if (line.Length != 0)
        Console.WriteLine(RecordReader.ParseLine(line).Kind);
```

This prints the same five kinds as the full program, one per line, without first loading all the records into a list.

## What is the difference between tracing and logging?

Use the JSONL file to inspect what happened inside one OneLLM instance in order. It includes message records, so it can hold application input and model replies. It’s not the saved state you’d use to resume a run, and it doesn’t export a distributed trace. Use [OneLLM’s OpenTelemetry spans](https://recursiveai.org/onellm/docs/telemetry/#spans) when you want to see where model calls fit into a trace of a larger operation. Those spans don’t contain prompts, completions, or tool arguments. You can use both: traces to follow a request across services, and local records to inspect a specific instance.
