Accessing Session Object inside Application_Error in Global.Asax RRS feed

  • Question

  • User1738843376 posted


    I'm attempting to access the Session Object from within the Application_Error of the Global.Asax, but i keep getting "Object reference not set to an instance of an object.".

    Here's my Application_Error code:

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim exc As Exception = Server.GetLastError
            If Not ErrorAlreadyLogged(exc.StackTrace.ToString()) Then
    	    Dim context As HttpContext = (CType(sender, HttpApplication)).Context
    	    exceptionUtility.logInfo("lang: " & context.current.session("Language"))
                LogError(exc, context)
            End If
        Catch ex As Exception
            ExceptionUtility.LogInfo("Error: " & ex.Message)
            ExceptionUtility.LogInfo("Error Details: " & ex.StackTrace.ToString())
        End Try
    End Sub

    I need to access the Session object so that i can capture details relative to the session, like the language being used when he website broke, or the userId (which i store in the session object) if logged in on the website when the error was thrown.

    Any ideias on this?


    Tuesday, October 9, 2018 1:00 PM

All replies

  • User465171450 posted

    Depending on which error it is, the session object may not be available as it hasn't been spun up yet. It's not available immediately in the lifecycle such as you would see in Begin_Request

    Tuesday, October 9, 2018 1:03 PM
  • User1738843376 posted

    Hi Markfitzme,

    Thx for the reply.

    So you're saying that there is no way to always catch values from the session object when inside the Application_Error sub?

    My goal is to log to the database every error that happens on the website, and if that error hasn't been reported in the last 30mins, then send an email to me, otherwise, don't send the email.

    Some of the data i wish to log on the database is only present on the session object, such as the language the user is using to navigate the website, the user id if logged in, etc.

    How can i otherwise capture these?

    Tuesday, October 9, 2018 2:45 PM
  • User753101303 posted


    The keyword here is "always".  The error could happen when session or even the current context is not available (for example in Application_Start or Session_End). Try to see what is null exactly and add the proper check to skip accessing the session or even the context when it is not available. For example (pseudo-code) :

    ' Log basic exception details
    If context.current is nothing then ' log that current.context is nothing and exit
    if context.current.session is nothing then 'log that session is, nothing and exit
    ' here you should be able to log session information

    BTW I prefer to use what ASP.NET offers out of the box ie User.Identity.Name should tell which user is logged.

    Tuesday, October 9, 2018 4:21 PM
  • User-893317190 posted

    Hi 0belix,

    The session is accessible after the AcquireRequestState event in global.asax , may be your error  is thrown before the event.

    So if you want to access session in the Application_Error  event, you could check whether the session is null.

    If you want to get the language the user is using , you could get it through  Thread.CurrentThread.CurrentCulture.Name

    For more information about AcqureRequestState and other events , you could refer to  


    Best regards,

    Ackerly Xu

    Wednesday, October 10, 2018 6:37 AM