Logowanie błędów Clientside za pomocą Elmah
Używam ELMAH do logowania błędów.NET. Działa świetnie, ale chcę rozszerzyć rejestrowanie błędów o błędy po stronie klienta, czyli arbitralne błędy JavaScript. Mogę przechwycić błędy za pomocą okna.Zdarzenie onerror, a następnie wywołanie obsługi. NET (.ashx) aby zalogować błąd w elmah, ale to tylko mój mały hack, aby rozwiązać problem. Czy jest lepszy sposób na zarejestrowanie błędu po stronie klienta do elmah?
4 answers
Oto bardziej kompletne rozwiązanie (myślę, że chwyciłem go ze strony ELMAH... nie pamiętam)
ErrorLogging.js:
window.onerror = function (msg, url, line) {
logError(msg, arguments.callee.trace());
}
function logError(ex, stack) {
if (ex == null) return;
if (logErrorUrl == null) {
alert('logErrorUrl must be defined.');
return;
}
var url = ex.fileName != null ? ex.fileName : document.location;
if (stack == null && ex.stack != null) stack = ex.stack;
// format output
var out = ex.message != null ? ex.name + ": " + ex.message : ex;
out += ": at document path '" + url + "'.";
if (stack != null) out += "\n at " + stack.join("\n at ");
// send error message
jQuery.ajax({
type: 'POST',
url: logErrorUrl,
data: { message: out }
});
}
Function.prototype.trace = function()
{
var trace = [];
var current = this;
while(current)
{
trace.push(current.signature());
current = current.caller;
}
return trace;
}
Function.prototype.signature = function()
{
var signature = {
name: this.getName(),
params: [],
toString: function()
{
var params = this.params.length > 0 ?
"'" + this.params.join("', '") + "'" : "";
return this.name + "(" + params + ")"
}
};
if (this.arguments)
{
for(var x=0; x<this.arguments.length; x++)
signature.params.push(this.arguments[x]);
}
return signature;
}
Function.prototype.getName = function()
{
if (this.name)
return this.name;
var definition = this.toString().split("\n")[0];
var exp = /^function ([^\s(]+).+/;
if (exp.test(definition))
return definition.split("\n")[0].replace(exp, "$1") || "anonymous";
return "anonymous";
}
Układ / Strona Wzorcowa:
<script src="@Url.Content( "~/Scripts/ErrorLogging.js" )" type="text/javascript"></script>
<script type="text/javascript">
//This needs to be here to be on everypage
var logErrorUrl = '@Url.Action( "LogJavaScriptError", "Home" )';
</script>
HomeController.cs:
[HttpPost]
public void LogJavaScriptError( string message ) {
ErrorSignal.FromCurrentContext().Raise( new JavaScriptErrorException( message ) );
}
JavaScriptErrorException.cs:
[Serializable]
public class JavaScriptErrorException: Exception{
public JavaScriptErrorException( string message ) : base ( message ){}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-13 23:16:44
Spójrz na hoptoadapp.com i jak robią raportowanie javascript.
To dokładnie to samo, ale inny backend : -)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-12-23 14:33:53
Wyłapuje wszystkie błędy javascript, obsługując okno.Zdarzenie onerror i wywołanie ajax do ręcznego logowania błędu w elmah
window.onerror = function (msg, url, linenumber) {
//make ajax call with all error details and log error directly into the elmah database
//show freindly error msg here to user
//hide error from browser
return true;
}
Nie znaleziono wówczas lepszego rozwiązania.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-06 13:57:55
Spójrz na to.
Http://joel.net/logging-errors-with-elmah-in-asp.net-mvc-3--part-5--javascript
Jedyna modyfikacja, jaką musiałem wprowadzić do powyższego kodu, dotyczyła wiązania.
Zastąpiłem
...z @ Scripts.Render ("~/stacktrace.js")
I w mojej konfiguracji bundle dodałem linię... paczki.Add (new ScriptBundle ("~/stacktrace.js").Include ("~/Scripts/stacktrace.js"));
To sprawia, że kod jest kompatybilny z nowszym MVC 4.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-14 04:18:27