将 C# 函数输出到 DocumentDB

Output C# Function to DocumentDB

这可能是因为我在 C# 方面不太出色(我已经到了)或者这方面的文档不太完整。

无论如何,我正在尝试获取一些 Azure 事件,然后将每个事件写入 DocumentDB。

public static void Run(TimerInfo myTimer, TraceWriter log, out ICollector<string> outputDocument)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

        JArray auditLogs = GetAuditLogsJArray(TenantId, AuditApiVersion, log);

        outputDocument = null;
        foreach (dynamic logevent in auditLogs)
        {
            object document = new
            {
                id = logevent.id,
                activityType = logevent.activityType,    
            };
            string json = JsonConvert.SerializeObject(document);
            outputDocument.Add(json);
        }
    }

我经常在最后一行收到 System.NullReferenceException,但我不知道为什么。 Visual Studio 调试显示 json 有一个值(json 字符串)。 如果我将代码更改为不是 ICollector 而只是 string 并将最后一行更改为 outputDocument = json; 然后函数完成并仅将一个文档添加到 DocumentDB,而不是所有文档(将近 500 个)。

我做错了什么?!

干杯 大卫

public static void Run(TimerInfo myTimer, TraceWriter log, ICollector<string> outputDocument)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

    JArray auditLogs = GetAuditLogsJArray(TenantId, AuditApiVersion, log);

    // outputDocument = null;
    // not out-param so it will be passed in.
    foreach (dynamic logevent in auditLogs)
    {
        object document = new
        {
            id = logevent.id,
            activityType = logevent.activityType,    
        };
        string json = JsonConvert.SerializeObject(document);
        outputDocument.Add(json);
    }
}