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 Answer
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>