views:

146

answers:

1

I need to log sessions to database on my asp.net website: who and when started and finished the session. I've configured odbc logging on my iis server but unfortunately there is no such information being logged (or I can't see it). How can I do it either on asp.net website (but simply because there are hundreds of pages and I can't modify each one) or my iis logging?

+4  A: 

I'd use a good logging library (Enterprise Library or Log4Net), and write logging code in the Session_Start and Session_End event handlers in Global.asax.

Be aware, however, that if you are using SqlServerSessionState, the Session_End event doesn't fire.

Edit: Actually, if you need to log the user name, you might find it more appropriate to log the session start from the Application_AuthenticateRequest, where the identity will have been established. This will not strictly be the start of the session, but the fact is that unless you are using integrated Windows security, the user's identity will not be established when the session is created.

Tor Haugen
+1 for calling out the fact that Session_End only fires if you're using InProc sessions (so SessionServer also has this limitation). Reliably getting the end of a users session is tricky.
Zhaph - Ben Duguid