如何发送Android应用程序日志,远程服务器? [英] How to send android app logs to remote server?
问题描述
在我的应用程序,我想在崩溃的情况下,以远程服务器发送日志。我已经加入try-catch块,并在追赶我送日志服务器。我想知道所有的异常,我应该追上。我需要每个记录崩溃的情况下,这样我可以修复它。难道是一个很好的做法,以捕获所有异常?
In my application, I want to send logs in case of crash to remote server. I have added try-catch block and in catch I'm sending logs to server. I want to know what all exceptions should I catch. I need logs in case of every crash so that I can fix it. Would it be a good practice to catch all Exceptions?
先谢谢了。
推荐答案
下面是我的建议从其他伟大的答案策划汇总列表:
Here is a summarized list of suggestions I curated from other great answers:
-
捕捉所有未处理的异常
-
创建一个
的ExceptionHandler
实现java.lang.Thread.UncaughtExceptionHandler
。你可以使用这个类来定义和记录错误。
Create an
ExceptionHandler
that implementsjava.lang.Thread.UncaughtExceptionHandler
. You can use this class to customize and log errors.
将这个: Thread.setDefaultUncaughtExceptionHandler(新的UncaughtExceptionHandler(本));
在的OnCreate()
方法(在超()
)每个活动
类的。
Put this: Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
inside OnCreate()
method (after super()
) of each of your Activity
class .
有关详细信息,请参阅本伟大的回答 Android的异常处理的最佳实践。
For details, please refer to this great answer from Android exception handling best practice .
括周围的所有codeS从外部外部/运行try / catch语句。 (例如 - codeS使用第三方组件)
括周围的所有codeS,你知道的,可以在一定条件下失败的try / catch
- 当没有互联网连接
- 在I / O操作
- 当是疯狂的用户输入的潜力
- 当有对潜在除以零的情况下
使用的try / catch在顶层,因为所有的异常都冒泡到顶级。因此,在内部函数,如果需要的话,可以考虑使用扔
例外。然后,在顶层,捕获特定的异常和排序的异常类型在catch块正确。 (简称从这个伟大的回答:<一href=\"http://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice\">How使用尝试捕捉异常处理是最好的做法
Use try/catch at the top level because all exceptions are bubbled up to the top level. So, in inner functions, if need be, you can consider using throw
exception. Then, at the top level, catch specific exceptions and sort your exception types in catch block correctly. (Referred from this great answer: How using try catch for exception handling is best practice
有关UI,你应该提供持续可用性提供,即使出现错误(如果错误是由于,例如,没有互联网连接)的应用程序功能有限的版本。
For UI, you should offer "continued usability" by providing "limited functionality" version of the app even if the error occurs (if the error is due to, for example, no internet connectivity).
过于急切的错误给用户报告不是一个好主意。
Over-eager error reporting to user is not a good idea.
持续出现错误信息是坏的。告知用户有一条消息,让用户关闭/删除它。
Persistent error messages are bad. Inform user with a message and let the user close/remove it.
<一个href=\"http://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice\">Reference 1 | <一href=\"http://stackoverflow.com/questions/16561692/android-exception-handling-best-practice\">Reference 2 | <一href=\"http://www.java$c$cgeeks.com/2013/07/java-exception-handling-tutorial-with-examples-and-best-practices.html\"相对=nofollow>参考3 | 延伸阅读1 | 进一步阅读2 | 进一步阅读3
Reference 1 | Reference 2 | Reference 3 | Further Reading 1 | Further Reading 2 | Further Reading 3
有关一个Android库远程登录未处理的异常,你可以在此看一看:的 Android的远程堆栈跟踪。
For an Android library to remotely log unhandled exceptions, you can take a look at this: android-remote-stacktrace .
- 的.jar文件
- 将它添加到Java构建路径。
-
添加INTERNET权限:使用许可权的android:NAME =android.permission.INTERNET对
- Download .jar file.
- Add it to Java Build path.
Add INTERNET permission: uses-permission android:name="android.permission.INTERNET"
在您的活动或服务,把'ExceptionHandler.register的的OnCreate
方法(这一点,的 HTTP://your.domain/path );
In the OnCreate
method of your activity or service, put 'ExceptionHandler.register(this, "http://your.domain/path");'
这篇关于如何发送Android应用程序日志,远程服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!