Detecting browser close (X) event in ASP.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I'm writing trace log of various actions happening in my ASP.NET web
application by opening a text file when the user logs in to the system and
closing the file while the user logs out. This is especially to nail a
hangout issue.

The problem I'm facing is, when the application hangs and the user closes
the browser using close(X) button I'm not able to close the text
file(handle). This keeps the file handle open and the contents of the file
will be blank if viewed from Windows explorer.

Is there any way to resolve this by executing the statements to close the
file even if the user exits the application by closing the browser and not by
normal logout.

Your valuable ideas/solutions are welcome!

Thanks,
Mammen
 
Hi,

You could possibly catch IE's document.onbeforeunload event in
javascript causing a postback before termination. (this would probably
only work in newer ie browsers).

HTH
 
Hello,

There is no special ASP.NET event for that.

You can do the following:

1.) I think there is a javascript/jscript event fired in body called
"onclose" (please have a look at the web).

2.) Close your file when the session times out -> ASP.NET Event
"Session_OnEnd" (e.g. in VB.NET)

3.) Use the powerful ASP.NET built-in trace functionality instead of
writing your own. There is also a tool called log4net available for
tracing/logging string to files/databases and so on.
Please also look at the web for this.

Bet regards
Sven
 
mammen said:
I'm writing trace log of various actions happening in my ASP.NET web
application by opening a text file when the user logs in to the
system and closing the file while the user logs out. This is
especially to nail a hangout issue.

How about only opening the file when you need to write to it and closing it
immediately? Maybe it's that file being open that's causing the hang.
It's as simple as

Dim fsw As New FileStream(currentLog, FileMode.OpenOrCreate,
FileAccess.Write, FileShare.Read)
fsw.Position = fsw.Length
Dim w As StreamWriter = New StreamWriter(fsw)
w.WriteLine(DateTime.Now.ToLongTimeString() & vbTab & logData)
w.Close()
The problem I'm facing is, when the application hangs and the user
closes the browser using close(X) button I'm not able to close the
text file(handle). This keeps the file handle open and the contents
of the file will be blank if viewed from Windows explorer.

Where is the hang happening? Is there some client-side JS going wrong, or is
it on the server?

If the client has hung, it cannot tell the server.
If the server has hung, it cannot close the file.
Is there any way to resolve this by executing the statements to close
the file even if the user exits the application by closing the
browser and not by normal logout.

Anyway, the network connection could be dropped with no action from the
server or user.

If you look in microsoft.public.inetserver.iis for some really big posts,
you'll find what debugging tool they're using to generate the log and see
that there are helpful people there who will analyze the log to see what
exactly caused the hang.

HTH

Andrew
 
Hi Andrew/Sven,

Thanks for your quick and detailed response.

I have decided to include more information in the application trace I'm
generating and analyse it using the trace utility "trace.axd" available with
..NET.

Thanks,
Mammen
 
Back
Top