How to debug CosmosDB stored procedure easily

CosmosDB stored procedure, written in javascript, sometimes can be difficult to debug. One easy way is to do console log like normal javascript and debug the code with those logs.

Console logs are by default disabled in CosmosDB stored procedures. You have to enable them first before making the request to CosmosDB.


var options = new RequestOptions()
        {
          PartitionKey = new PartitionKey(partitionKey.ToString()),
          EnableScriptLogging = true
        };

StoredProcedureResponse<TValue> result = await 
  clientMgr.DocumentClient.ExecuteStoredProcedureAsync<TValue>(
          clientMgr.GetStoredProcUri(storedProcName),
          options,
          procedureParams);

Here, TValue = your custom response object

You can now access the console log in your stored procedure written in javascript inside StoredProcedureResponse.ScriptLog like this

Console.WriteLine(result.ScriptLog)