简体   繁体   English

在服务器上记录客户端JavaScript错误

[英]Logging Client side JavaScript Errors on Server

使用功能名称,行号,文件名在服务器上记录客户端JavaScript错误。在页面加载和单击按钮时处理错误。如何编写错误日志。

You can create a controller ErrorController with Log action, in this controller you save error information to DB using Entity Framework. 您可以使用Log操作创建一个控制器ErrorController ,在该controller您可以使用Entity Framework将错误信息保存到DB。

 [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. 如果您担心安全测试,则可以添加属性Authorize on action。

In client side implement onerror event 在客户端实施onerror事件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM