A day with .Net

My day to day experince in .net

Archive for July, 2015

JavaScript Logging in ASP.NET MVC – Jsnlog and elmah

Posted by vivekcek on July 25, 2015

Hi Guys in this post i will show you, how you can log javascipt erros in your asp.net mvc applications.

First of all, let me tell you the tools.

1. Visual Studio 2013
2. ASP.NET MVC 4
4. Elmah.mvc
5. Jsnlog.elmah
6. SQL Server 2008

So we can start

1. Create an MVC application.

2. Got to Nugget and add Elmah.MVC, Please note i using elmah’s mvc package

1

3. Now go to elmah site and download sql scripts for elmah

2

4. Create a database named Logging and apply the downloaded script.

3
5. Go to web.config and add connection string for elmah

4

6.Configure elmah in web.config by adding this

 <elmah>
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah" />
     <security allowRemoteAccess="false" />
  </elmah>

5

7. Great now thorw an exception from your home controller

6

8. Go to elmah, if you saw this screen, we are good, elmah is configured correctly
7

9. Again back to Nugget and add Jsnlog for elmah

8

10. Go to your _Layout.csftml and add this at top

@Html.Raw(JSNLog.JavascriptLogging.Configure())
    <script type="text/javascript">
        window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {

            // Send object with all data to server side log, using severity fatal,
            // from logger "onerrorLogger"
            JL("onerrorLogger").fatalException({
                "msg": "Exception!",
                "errorMsg": errorMsg, "url": url,
                "line number": lineNumber, "column": column
            }, errorObj);

            // Tell browser to run its own error handler as well
            return false;
        }
    </script>

9

11. Make some javascript error in index.cshtml

@section scripts{
    <script type="text/javascript">
        JL().info("This is a log message");
        xyz;
    </script>

}

10

12. Go to elmah and watch this

11

Advertisements

Posted in javascript, MVC | Tagged: , , , , , | Leave a Comment »