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.

1

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.