Documentation Menu

RequestId Method

Returns the current request id.

Definition

// For ASP.NET 4.x
public static string RequestId()
    
// For ASP.NET Core    
public static string RequestId(this HttpContext httpContext)

Return Value

A string identifying the current request.

Remarks

This method returns the request id of the current request (details). Including this string in your server side log entries allows you to track which log entries belong to the same request (how).

Each time you call this method within the same request, you will get the same string.

Note that if you call the Configure HTML helper (for ASP.NET 4.x) or jl-javascript-logger-definitions tag helper (for ASP.NET Core) with your own request id, that doesn't mean the RequestId method from that point on returns your request id. RequestId always returns the same request id.

How the request id is found

This method first checks whether the current request is a log request from the jsnlog.js library. If it is, it returns the request id contained in the log request. This way, the request ids in the client side log requests match the request id of the original request to the server that loaded the page that is now sending those log requests.

If it is not a log request, this method comes up with a unique id for the current request.

On ASP.NET 4.x, if you have Event Tracing for Windows (ETW) switched on, JSNLog uses the same request id that is used internally by IIS.

On ASP.NET Core, it looks at the TraceIdentifier property of the HttpContext.

If the method can't get the request id with the above means, it generates a random GUID.