如何发送Android应用程序日志到远程服务器? [英] How to send android app logs to remote server?

查看:213
本文介绍了如何发送Android应用程序日志到远程服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想将事件发送到远程服务器。我添加了try-catch块,在catch我正在发送日志到服务器。我想知道我应该捕捉到什么异常。我需要日志,以防每次崩溃,以便我可以修复它。捕获所有异常情况会很好吗?



提前感谢

解决方案




  • 捕获所有未处理的异常


    1. 创建一个 ExceptionHandler 实现 java.lang.Thread。的UncaughtExceptionHandler 。你可以使用这个类自定义和记录错误。


    2. 将这个: Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this)); / code>在 OnCreate()方法( super()之后) $ c>活动类。


    3. 有关详情,请参阅 Android异常处理最佳实践



  • 尝试/捕获从外部/外部运行的所有代码。 (例如 - 使用第三方组件的代码)


  • 将所有知道的代码包含在try / catch中,可能会在某些情况下失败


    1. 当没有互联网连接时

    2. 在I / O操作期间

    3. 当有潜在的疯狂的用户输入

    4. 当有可能除以零情况

    / li>

  • 使用顶层的try / catch ,因为所有异常都冒泡到顶层。因此,在内部函数中,如果需要,可以考虑使用 throw exception。然后,在顶层,捕获特定异常和正确地在catch块中排除异常类型。 (参考这个很棒的答案:如何使用try catch异常处理是最佳实践


  • 对于UI,您应该通过提供有限功能版本的应用程序来提供持续可用性,即使发生错误(如果错误是由于例如无互联网连接造成的)。


  • 向用户提供过度的错误报告不是一个好主意。 / p>


  • 持续的错误消息是坏的,通知用户一个消息,让用户关闭/删除它。




参考1 | 参考2 | 参考3 | 进一步阅读1 | 进一步阅读2 | 进一步阅读3






对于Android库来远程记录未处理的异常,您可以查看以下内容: android-remote-stacktrace




  • 下载 .jar 文件。

  • 将其添加到Java Build路径。

  • 添加INTERNET权限:uses-permission android:name =android.permission.INTERNET


  • 在您的活动或服务的 OnCreate 方法中,将ExceptionHandler.register(this, http://your.domain/path );'


  • 在你的服务器,地点这个PHP文件,您可以编辑它来扩展功能。


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?

Thanks in advance.

解决方案

Here is a summarized list of suggestions I curated from other great answers:

  • Catch all unhandled exceptions.

    1. Create an ExceptionHandler that implements java.lang.Thread.UncaughtExceptionHandler. You can use this class to customize and log errors.

    2. Put this: Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this)); inside OnCreate() method (after super()) of each of your Activity class .

    3. For details, please refer to this great answer from Android exception handling best practice .

  • Enclose try/catch around all codes that run externally/from outside. (e.g - codes using 3rd party components)

  • Enclose try/catch around all codes that you know, can fail on certain conditions

    1. When there is no internet connections
    2. During I/O Operations
    3. When there is potential for crazy user inputs
    4. When there is potential for divide by zero situations

  • 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

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

Reference 1 | Reference 2 | Reference 3 | Further Reading 1 | Further Reading 2 | Further Reading 3


For an Android library to remotely log unhandled exceptions, you can take a look at this: android-remote-stacktrace .

  • Download .jar file.
  • Add it to Java Build path.
  • Add INTERNET permission: uses-permission android:name="android.permission.INTERNET"

  • In the OnCreate method of your activity or service, put 'ExceptionHandler.register(this, "http://your.domain/path");'

  • On your server, place this PHP file, which you can edit to extend the functionality.

这篇关于如何发送Android应用程序日志到远程服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆