On this repository, you will have an .Net Core 2.0 WebJob using Dependency Injection and Application Insights which will listen CosmosDB data modifications.
For more information about :
- how to convert a .Net Core 2.0 App Console to a WebJob, read DotNetCore-AppConsole-Azure-WebJob,
- how to configure the DocumentDB on CosmosDB, read Azure Cosmos DB: Build a DocumentDB API web app with .NET and the Azure portal
On https://portal.azure.com/ create a new 'Function App'. Then add a new Function based on 'CosmosDbTrigger'.
Go to DocumentDB settings then click on 'Add Azure Function'. Select the Collection you want to monitor, the App function you have just created and the other fields then click on 'Save'.
Here is the code of the function you could use:
#r "Microsoft.Azure.Documents.Client"
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
public static void Run(IReadOnlyList<Document> input, ICollector<string> returnValues, TraceWriter log)
{
var count = (input != null) ? input.Count : 0;
log.Verbose($"There are {count} document(s).");
for (var i = 0; i < count; i++)
{
log.Verbose($"input[{i}] = '{input[i]}'");
returnValues.Add($"{input[i]}");
}
}
It will allow you to log the data entered in the database.
In 'Integrate' section, add a new Output, select 'AzureQueueStorage' and complete the field before saving. Note: The default Queue Name is 'outqueue'. Please it like this to trigger the command in the app.
Final step, run the App