Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I'm tring to Create HttpModlue to trace userInfo when web application starts.
However I have some problems where I tried ot call MappingUserInfo() under Application_BeginRequest.
When I tried to call MappingUserInfo(), there is null
VB
CType(HttpContext.Current.Cache(uInfo.CacheKey), UserInfo).SessionID
which I already get the sessionID in
VB
Application_PreRequestHandlerExecute


It worked fine when I do Session_Start with Global.ascx but I would like to some work in HttpModule.
Any help would be greatly appreciated.

VB
' In the Init function, register for HttpApplication events by adding your handlers.
    Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
        AddHandler application.PreRequestHandlerExecute, AddressOf Me.Application_PreRequestHandlerExecute
        AddHandler application.BeginRequest, AddressOf Me.Application_BeginRequest
        AddHandler application.EndRequest, AddressOf Me.Application_EndRequest

    End Sub

    ' Your BeginRequest event handler.
    Private Sub Application_BeginRequest(ByVal [source] As [Object], ByVal e As EventArgs)
        Dim application As HttpApplication = CType([source], HttpApplication)
        Dim context As HttpContext = application.Context

       'Call MappingUserInfo
        MappingUserInfo()
    End Sub

    ' Your EndRequest event handler.
    Private Sub Application_EndRequest(ByVal [source] As [Object], ByVal e As EventArgs)
        Dim application As HttpApplication = CType([source], HttpApplication)
        Dim context As HttpContext = application.Context
    End Sub


    'This event is called for every request just prior to the HTTPHandler 
    Private Sub Application_PreRequestHandlerExecute(ByVal [source] As [Object], ByVal e As EventArgs)
        Dim application As HttpApplication = CType([source], HttpApplication)
        Dim handler As IHttpHandler = application.Context.Handler

        'To check if the handler is a Page Class and Handle the PreRender event of the page
        If TypeOf handler Is Page Then
            '((Page)handler).PreRender +=new EventHandler(CustomModule_PreRender); 
            AddHandler DirectCast(handler, System.Web.UI.Page).PreRender, AddressOf Me.CustomModule_PreRender

        End If
    End Sub

    Private Sub CustomModule_PreRender(ByVal [source] As [Object], ByVal e As EventArgs)

        Try
            Dim uInfo As New UserInfo()
            uInfo.SessionID = HttpContext.Current.Session.SessionID

           'do nothing
        Catch ex As Exception
        End Try
    End Sub



    Private Sub MappingUserInfo()
        Try
            Dim uInfo As New UserInfo()
            uInfo.SessionID = DirectCast(HttpContext.Current.Cache(uInfo.CacheKey), UserInfo).SessionID
            uInfo.UserName = WindowsIdentity.GetCurrent().Name 'HttpContext.Current.User.Identity.Name '
            uInfo.StartTime = DateTime.Now.ToString()
            uInfo.MachineName = GetMachineName
            uInfo.IpAddress = GetIpaddress

            Dim SessTimeOut As New TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0)


            HttpContext.Current.Cache.Insert(uInfo.CacheKey, uInfo, Nothing, DateTime.MaxValue, SessTimeOut)
        Catch ex As Exception
            Throw New Exception("Error in Session_Start function", ex)
        End Try

    End Sub
Posted
Updated 12-Apr-12 7:18am
v3
Comments
ZurdoDev 12-Apr-12 11:32am    
added code tags
ZurdoDev 12-Apr-12 11:32am    
what's the question?
deepakaitr12345 13-Apr-12 8:06am    
YOu can use
<trace enabled="true" traceMode="SortByTime" pageOutput="true" localOnly="false" mostRecent="true" requestLimit="40"/>

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900