{"record":{"id":"1d2ffa1316501d1e","repo":"spectreconsole/spectre.console","slug":"cannot-export-text-since-a-recording-hasn-t-been-s","errorCode":null,"errorMessage":"Cannot export text since a recording hasn't been started.","messagePattern":"Cannot export text since a recording hasn't been started\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console/AnsiConsole.Recording.cs","lineNumber":27,"sourceCode":"    /// Starts recording the console output.\n    /// </summary>\n    public static void Record()\n    {\n        if (_recorder == null)\n        {\n            _recorder = new Recorder(Console);\n        }\n    }\n\n    /// <summary>\n    /// Exports all recorded console output as text.\n    /// </summary>\n    /// <returns>The recorded output as text.</returns>\n    public static string ExportText()\n    {\n        if (_recorder == null)\n        {\n            throw new InvalidOperationException(\"Cannot export text since a recording hasn't been started.\");\n        }\n\n        return _recorder.ExportText();\n    }\n\n    /// <summary>\n    /// Exports all recorded console output as HTML text.\n    /// </summary>\n    /// <returns>The recorded output as HTML text.</returns>\n    public static string ExportHtml()\n    {\n        if (_recorder == null)\n        {\n            throw new InvalidOperationException(\"Cannot export HTML since a recording hasn't been started.\");\n        }\n\n        return _recorder.ExportHtml();\n    }","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console/AnsiConsole.Recording.cs#L9-L45","documentation":"Thrown by AnsiConsole.ExportText() when no Recorder is attached, i.e. AnsiConsole.Record() was never called. Exporting requires an active recorder that has been capturing writes, so exporting without one is an invalid state (InvalidOperationException).","triggerScenarios":"Calling AnsiConsole.ExportText() before AnsiConsole.Record().","commonSituations":"Forgot to call Record(); conditional code path that exports in a branch where recording never started; reordered calls after a refactor; test that exercises export without setup.","solutions":["Call AnsiConsole.Record() before any output you want to capture, then ExportText()","Maintain your own flag tracking whether Record() ran, and guard the export call","Wrap ExportText() in try/catch(InvalidOperationException) if recording is optional"],"exampleFix":"// before\nAnsiConsole.WriteLine(\"hello\");\nvar text = AnsiConsole.ExportText(); // throws\n\n// after\nAnsiConsole.Record();\nAnsiConsole.WriteLine(\"hello\");\nvar text = AnsiConsole.ExportText();","handlingStrategy":"try-catch","validationCode":"// No public API exposes recording state, so track it yourself\nbool recording = false;\nAnsiConsole.Record();\nrecording = true;\nAnsiConsole.WriteLine(\"hello\");\nif (recording)\n    var text = AnsiConsole.ExportText();","typeGuard":null,"tryCatchPattern":"try\n{\n    var text = AnsiConsole.ExportText();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"recording hasn't been started\"))\n{\n    // recording was never started; handle gracefully\n}","preventionTips":["Always call AnsiConsole.Record() before the output you intend to capture","Keep a local bool mirroring recording state since the library does not expose it","Centralize start/export calls in one helper to avoid mismatched ordering"],"tags":["recording","state","usage","export"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}