定时器触发 Azure 函数 Activity.Current 为空

问题描述

场景

我已关注此 tutorial 并在 azure function v3 中创建了 net core 3.1 来监控我的网络应用程序。您使用了 Visual Studio,而不是在门户中进行开发。

问题

不幸的是,我收到了 System.NullReferenceException: Object reference not set to an instance of an object.System.Diagnostics.Activity.Current.get returned null.

availability.Context.Operation.ParentId = Activity.Current.SpanId.ToString();

如何解决这个问题?

解决方法

嗯...我觉得教程可能在代码上有一些错误,我们可以将发生错误的两行注释掉。

这个doc说:

Activity 应该通过调用构造函数来创建,配置为 必要,然后从 Start 方法开始,该方法维护 活动和集合的父子关系 活动。当前。

这就是 availability.Context.Operation.ParentId = Activity.Current.SpanId.ToString(); 突发错误的原因。并专注于这个行代码, 这条线有意义吗?所以在我的测试中,我把这行和后面的行注释掉了,代码执行得很好。

enter image description here enter image description here