Browser crash detection server side c#

Viewed 66

As when browser or page crashes I need to detect it in the server-side what I actually found is this solution so far when the browser closes it reads as below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication2010.WebForm3" %>
<%@ Register Assembly="AjaxControlToolkit, Version=3.0.30512.17815, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
    Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Handling Browser Close Event at Server Side: ASP.NET</title>
</head>
<body onunload="deleteRecord();">
    <script type="text/javascript" language="javascript">
        function deleteRecord() {
         //this method will call server-side method            
            PageMethods.deleteRecord();
        }
    </script>
    <form id="form1" runat="server">
    <div id="div1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        Click on Close Button
    </div>
    </form>
</body>
</html>

This is the client page above and the server code is below written.

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static void deleteRecord()
{
    // You can write any logic you want to perform when the browser is getting closed
    System.Diagnostics.Debug.Write("You can write your logic here");
}

Here is the excerpt which I used - https://www.dotnetspider.com/resources/30715-Handling-Browser-Close-Event-at-Server-Side.aspx
Any suggestions might help out in this browser crash context.

1 Answers

Check the comments below the post

Not only onunload will be called on browser closed, It will also be called whenver postback your page.

Therefore the deleteRecord() method will be called for all postback.

Also onunload will be fired twice when you click link buttons.

You can also not be sure that anything will run on crash. If an exception is thrown on the browser, there is no crash event that is raised. More often than not a crash will stop the javascript altogether, so no ajax will run.

Best bet for client side errors is to use something like application insights to send error telemetry on javascript errors.

Check client side monitoring here: https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net#add-client-side-monitoring

Related