Skip to content

Add --diagnostics option for capturing shell diagnostic logs (#122)#127

Open
mkrueger wants to merge 2 commits into
mainfrom
dev/mkrueger/diagnostics-logging
Open

Add --diagnostics option for capturing shell diagnostic logs (#122)#127
mkrueger wants to merge 2 commits into
mainfrom
dev/mkrueger/diagnostics-logging

Conversation

@mkrueger

@mkrueger mkrueger commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements issue #122. Adds a --diagnostics [path] CLI option that writes a timestamped diagnostic log file so users can capture shell activity for troubleshooting, performance analysis, and auditing.

This is the file-based diagnostic logging that was deliberately left out of scope of the distributed-tracing PR (#126); the two features are independent.

CLI

cosmosdbshell --diagnostics              # log to a timestamped file in the config directory
cosmosdbshell --diagnostics mylog.log    # log to a custom file path

--diagnostics takes an optional value (mirroring --mcp). With no value it writes to diagnostics-<yyyyMMdd-HHmmss>.log in the shell configuration directory; with a value it writes to that path.

What gets logged

  • Session metadata header: start timestamp, machine name, OS, runtime version
  • Connection events: endpoint URL and connection mode
  • Command execution: every command with a timestamp
  • Command results: [OK]/[FAIL] with elapsed time in milliseconds
  • Errors: exception type and message for failed commands

Sample output

# CosmosDB Shell Diagnostic Log
# Started: 2026-06-11T14:30:00.0000000-07:00
# Machine: DEVBOX
# OS: Microsoft Windows NT 10.0.22631.0
# Runtime: 10.0.0
--------------------------------------------------------------------------------
[14:30:01.234] [CONNECT ] https://myaccount.documents.azure.com:443/ (mode=Direct)
[14:30:02.456] [CMD     ] dir
[14:30:02.789] [RESULT  ] [OK] 333.2ms | dir
[14:30:05.100] [CMD     ] cat nonexistent
[14:30:05.102] [ERROR   ] cat nonexistent -> ShellException: Item not found
[14:30:05.102] [RESULT  ] [FAIL] 2.1ms | cat nonexistent

Changes

  • New Core/DiagnosticLog.cs: thread-safe file writer; serializes writes, swallows IO failures so diagnostics never disrupt the session.
  • ShellInterpreter: EnableDiagnostics(path), per-command timing + result/error logging in ExecuteCommandAsync, connection event in Connect, disposal in Dispose.
  • Program.cs: --diagnostics [path] option + plumbing; CosmosShellOptions.EnableDiagnostics/DiagnosticsPath.
  • help-Diagnostics, diagnostics-enabled, diagnostics-error-create strings in en.ftl.
  • README.md, docs/navigation.md, and Runtime/DiagnosticLogTests.cs.

Validation

  • Build: 0 warnings, 0 errors.
  • Full test suite: 1298 passed, 0 failed, 74 skipped (pre-existing emulator-dependent skips).

Implements issue #122. Adds a --diagnostics [path] CLI option that writes a timestamped diagnostic log file capturing session metadata, command execution + timing, OK/FAIL results, errors, and connection events.

- New Core/DiagnosticLog.cs: thread-safe file writer with session header and tagged entries (CONNECT/CMD/RESULT/ERROR)
- ShellInterpreter: EnableDiagnostics(path), per-command timing + result/error logging in ExecuteCommandAsync, connect event in Connect, disposal in Dispose
- Program.cs: --diagnostics [path] option (optional value, like --mcp); defaults to a timestamped file in the config directory
- help-Diagnostics, diagnostics-enabled, diagnostics-error-create strings in en.ftl
- README, docs/navigation.md, and Runtime/DiagnosticLogTests.cs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in --diagnostics [path] CLI option to persist a timestamped, session-scoped diagnostic log capturing shell activity for troubleshooting/auditing.

Changes:

  • Introduces a new DiagnosticLog file writer and wires it into ShellInterpreter (command/connect/result/error logging + lifecycle management).
  • Adds --diagnostics [path] parsing/plumbing in Program.cs plus localized help/output strings.
  • Updates user docs (README.md, docs/navigation.md) and adds runtime tests for the new logging behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
README.md Documents the new --diagnostics [path] startup option.
docs/navigation.md Adds --diagnostics to navigation docs + usage examples.
CosmosDBShell/Program.cs Defines/parses --diagnostics [path] and enables diagnostics before startup connect.
CosmosDBShell/lang/en.ftl Adds localized help text and enable/error messages for diagnostics.
CosmosDBShell/Azure.Data.Cosmos.Shell.Core/ShellInterpreter.cs Emits diagnostics entries for command execution/results and connection events; disposes the log writer.
CosmosDBShell/Azure.Data.Cosmos.Shell.Core/DiagnosticLog.cs Implements the file-based diagnostic log writer and log entry formatting.
CosmosDBShell.Tests/Runtime/DiagnosticLogTests.cs Adds tests for log formatting and interpreter integration.

Comment on lines +336 to +343
stopwatch.Stop();
var succeeded = !(result?.IsError ?? true);
if (!succeeded && result is ErrorCommandState errorState)
{
diagnostics.LogError(command, errorState.Exception);
}

diagnostics.LogResult(succeeded, stopwatch.Elapsed.TotalMilliseconds, command);
Comment on lines +132 to +140
private static string Flatten(string value)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}

return value.Replace("\r\n", " ").Replace('\r', ' ').Replace('\n', ' ').Trim();
}
Addresses Copilot review feedback on PR #127:

- Redact AccountKey= values before writing command text to the diagnostic log to avoid leaking connection-string secrets to disk.

- Log canceled commands as [CANCELLED] instead of [OK] so the diagnostic/audit log is not misleading.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants