Application_BeginRequest didn't fire for few request in MVC

shakti singh 21 Reputation points
2021-06-02T10:26:08.867+00:00

In our API when we are writing the logs we are adding ActivityId. It is created in Application_BegingRequest but for few call it didn't write any ActivityId and it seems it did not fire Application_BeginRequest method in Global.asax.
Please find the details below :

Code in Global.asax:

protected void Application_BeginRequest(object sender, EventArgs e)
{
Trace.CorrelationManager.ActivityId = Guid.NewGuid();
}
configuration for logs:

<target name="logfile" xsi:type="File" fileName="./Logs/${shortDate}.log" layout="${time} | ${activityid} | ${identity} | ${callsite:className=true:includeSourcePath=true:methodName=true:includeNamespace=false} | ${level:uppercase=true} | ${message} ${exception}" />

Sceenshot of logs:
101649-download.png

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,331 Reputation points
    2021-06-02T14:42:07.297+00:00

    I'd wager it is calling it but CorrelationManager doesn't work the way you expected so you're making the assumption it isn't getting called. This is easy to verify though. Log a message on each call to BeginRequest to confirm it is actually calling it.

    CorrelationManager is thread specific. More correctly it uses the calling context to get the data stored in the current thread. Hence it can be used on different threads at the same time. Hence if your BeginRequest is called on thread A and the actual logging of the ID occurs on thread B then you'll get different IDs. I vaguely remember back when I originally looked at using CorrelationManager myself that for MVC it automatically started a logical operation in the pipeline. Therefore there is no reason to set the activity ID manually as it'll do it automatically but this might have been for .NET Core.

    So my recommendation is to confirm that the thread that is logging your messages (which appears to be async which means it isn't) is the same thread that started the request otherwise it won't work consistently. Now to be clear things like how the awaiter are configured and whether async calls are actually being run sync will have a dramatic impact on this so it might be hard to tell.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Chao Deng-MSFT 801 Reputation points
    2021-06-10T06:13:06.163+00:00

    Hi @shakti singh ,

    Apart from the BeginRequest, are other events firing?You can use this simpler method to test it again.

    protected void Application_BeginRequest(object sender, EventArgs e)  
        {  
            Response.Write("Hi");  
        }  
    

    You can look at this official Microsoft document:
    https://learn.microsoft.com/en-us/dotnet/api/system.web.httpapplication.beginrequest?redirectedfrom=MSDN&view=netframework-4.8
    Or is the environment you are using locally or on the server?If it is on the server, try adding the PrecompiledApp.config file on the server.


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    ChaoDeng

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.