0

Scenario

I have followed this tutorial and created azure function v3 in net core 3.1 to monitor my web application. U used Visual Studio instead of developing it in the portal.

Problem

Unfortunately i received System.NullReferenceException: Object reference not set to an instance of an object.System.Diagnostics.Activity.Current.get returned null. in line:

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

How to fix that?

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
zolty13
  • 1,943
  • 3
  • 17
  • 34
  • May I ask a question that why you wanna follow this tutorial to enable monitor? In my opinion, if you''d like to create a function to monitor your web app, you can just enable app insights when creating azure function and it will provide many features to capture telemetry from function. – Tiny Wang Jun 24 '21 at 02:23
  • It enable to set availabilitynij azure. Simple ping is not enough for my application. I need to know if my authorized request with sent file is properly executed – zolty13 Jun 24 '21 at 05:28

2 Answers2

1

Hmmm... I think the tutorial may have some error on the code, and we can comment the two lines where error took place.

This doc said :

Activities should be created by calling the constructor, configured as necessary, and then started with the Start method, which maintains parent-child relationships for the activities and sets Activity.Current.

and this is why availability.Context.Operation.ParentId = Activity.Current.SpanId.ToString(); burst an error. And focus on this line-code, does this line make sense? So in my test, I made this line and the line behind it commented, and code executed well.

enter image description here enter image description here

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
0

I followed the same tutorial to set up availability tests with Azure Function.

First, deleting these two lines will solve the error and the function will work

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

But in this way, with availability.Id = Activity.Current.SpanId.ToString(); The availability.Id sent to logs in application insight will always be 0000000000000. If you are not concerned with giving each availability test a unique ID, that's fine.

If you want, you can add a custom ID or identifier in availability.Message in json format.

curtispy
  • 156
  • 8