Android中的Crashlytics - 如何在一个地方捕获所有异常/崩溃 [英] Crashlytics in Android - how to capture all exceptions/crashes in one place

查看:622
本文介绍了Android中的Crashlytics - 如何在一个地方捕获所有异常/崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java(android,但这是一个java问题)我在应用程序类中创建了一个或多或少的默认异常处理程序,这是从一些SO线程中获取的:

Using Java (android but this is a java question ) i have created a default exception handler in the application class like this more or less which was taken from some SO thread:

    public class MyApplication extends Application {
@Override
    public void onCreate() {
        super.onCreate();
        AppInitialization();
    }

    private void AppInitialization() {
         defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
         Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
    }

    private UncaughtExceptionHandler defaultUEH;

    // handler listener
    private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            ex.printStackTrace();
            // TODO handle exception here
        }
    };
}

让我们谈谈我的目标是什么。我需要记录crashLytics崩溃报告的额外信息。所以任何时候发生异常钩子uncaughtException(Thread thread,Throwable ex)都会被调用。我认为这将适用于崩溃和异常(不确定)。

Lets talk about what my goal is. I need to record extra information for crashLytics crash reports. so anytime a exception occurs the hook uncaughtException(Thread thread, Throwable ex) will be called. i assume that will work for crashes and exceptions (not sure).

3个问题:


  1. 这将影响如何crashLytics因为im覆盖了默认异常处理程序。

  1. will this affect how crashLytics works since im overrding the default exception handler.

这是否允许应用继续 - 我不想要这个。我只想将其他地方的异常记录下来,就像defaultexceptionHandler不存在一样。否则,QA回归将很难看到错误,所以我们可以解决它们。如果我从uncaughtException(thread,ex)中调用defaultUEH.uncaughtException(thread,ex)将会保持循环?

Will this allow the app to continue - i dont want this. i only want to log the exception somewhere else and have it continue as if the defaultexceptionHandler did not exist. Otherwise, QA regression will have a hard time seeing the bugs so we can fix them. If i call defaultUEH.uncaughtException(thread, ex) from within uncaughtException(thread,ex) will it keep looping ?

什么线程与setDefaultUncaughtExceptionHandler相关联?这个工作适用于所有线程,例如或者主线程?

What thread is associated with setDefaultUncaughtExceptionHandler ? does this work for all threads for example or just main thread ?

所以要清楚,我需要一种方法发送关于每个崩溃事件/异常的附加信息。如何?

So to be clear, i need a way to send additional information on every crashlytics crash/exception. How ?

更新:我看到这里是否使崩溃报告的致命问题呢?

UPDATE: I see here that someone found a solution but using this method does it make crashlytics report the fatal issues as well ?

推荐答案

我找到了一个干净干净的方法,它测试好了。在崩溃报告中发送日志时要小心Crashlytics.log。似乎在版本Crashlytics 2.3.14.151上不能一致工作。我用Crashlytics.setString(key,value)代替。 crashlytics.log似乎工作正常与crashlytics.logException(..)虽然。

i found a way to do this cleanly and it tests ok. be careful with Crashlytics.log to send logs during a crash report. seems to not work consistently on version Crashlytics 2.3.14.151. I use Crashlytics.setString(key, value) instead. crashlytics.log seems to work fine with crashlytics.logException(..) though.

另一个问题是,它将在非正式的异常中工作。答案是是的,我自己测试了。

the other question i had was would it work on asychrnous exceptions. The answer is yes i tested it myself.

所以它捕获所有线程的所有异常。

so it catches all exceptions from all threads.

其余的想法类似于此处。该人正在defaultExceptionHandler上使用装饰器模式来向uncaughtException方法添加功能。

The rest of the idea is similar to here. The person is using a decorator pattern over the defaultExceptionHandler to add functionality to the uncaughtException method.

这是我如何增强它:

   public class DefaultUnCaughtExceptionHandlerDecorator implements UncaughtExceptionHandler {

   private UncaughtExceptionHandler mDefaultUncaughtExceptionHandler; //we will decorate the default handler to add functionality to it

   public DefaultUnCaughtExceptionHandlerDecorator(UncaughtExceptionHandler mDefaultUncaughtExceptionHandler) {
       this.mDefaultUncaughtExceptionHandler = mDefaultUncaughtExceptionHandler;
   }

   @Override
   public void uncaughtException(Thread t, Throwable e) {

       logToCrashLytics(); //add our crashlytics logging and keys here
       //  we have added our crashlytics extra data, now invoke Crashlytics
       mDefaultUncaughtExceptionHandler.uncaughtException(t, e);
   }

   private void logToCrashLytics() {

           Crashlytics.setUserIdentifier(Model.getUserID());//or whatever. this will show directly on crashlytics dashboard a pretty element

       Crashlytics.setString("Some useful Info","user is logged in"); // or whatever you want, such as printing out singleton managers references or model info
     //dont use Crashlytics.Log(..) here. it wont work consistent. 
   }

}

现在在你的子类的应用程序类AFTER crashlytics被安装这样做:

now in your subclass of Application class AFTER crashlytics is installed do this:

Thread.setDefaultUncaughtExceptionHandler(new DefaultUnCaughtExceptionHandlerDecorator(Thread.getDefaultUncaughtExceptionHandler()));

这篇关于Android中的Crashlytics - 如何在一个地方捕获所有异常/崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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