1

Logging Client side JavaScript Errors on Server with function name,Line number,File name.Handling error on page load and button click.How to write the error log.

naresh
  • 11
  • 4
  • 1
    Possible duplicate of [Logging Clientside JavaScript Errors on Server](https://stackoverflow.com/questions/119432/logging-clientside-javascript-errors-on-server) – Hasitha Mar 29 '19 at 07:29

1 Answers1

2

You can create a controller ErrorController with Log action, in this controller you save error information to DB using Entity Framework.

 [HttpPost]
 [Authorize]
 public JsonResult Log(ErrorModel error)
 {
       var message = error.message;
       var url = error.url;
       //dbContext save to Log table  
 }

If you concern about security testing, you can add attribute Authorize on action.

In client side implement onerror event

<script type="text/javascript">
window.onerror = function(msg, url, linenumber) {
    $.ajax({
    type: "POST",
    url: '/Error/Log',
    contentType: "application/json; charset=utf-8",
    data: { message: msg, url: url, linenumer: linenumber },
    dataType: "json",
    success: function() { // handle if need },
});
};
</script>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62