VivekDev
VivekDev

Reputation: 25437

Activity id in ASP.NET Core 2 application

I have created a new application in ASP.NET Core 2 with MVC.

enter image description here

Then, as I opened the Error Action Method in HomeController

public IActionResult Error()
{
    return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

I am not able to understand both Activities as well as HttpContext.TraceIdentifier.

What's itching me more is the question mark(?) after Activity.Current(Activity.Current?.Id). Some new syntax?

Upvotes: 1

Views: 3180

Answers (1)

jimSampica
jimSampica

Reputation: 12410

The ? is a null conditional operator. It's a new feature in c# 6 that makes null checking easier.

Activity.Current?.Id and HttpContext.TraceIdentifier in this context are unique tracking ids so you can correlate the request with logs and telemetry.

Generally, Activity.Current will be null if you're not using application insights or managing activities yourself, hence the ? being present.

Upvotes: 3

Related Questions