Send a log to Crashlytics without an app crash

♀尐吖头ヾ 提交于 2019-12-02 19:59:07
Tiago Almeida

With the new update from crashlytics you can now use:

[[Crashlytics sharedInstance] recordError:error];

And in Swift:

Crashlytics.sharedInstance().recordError(error)

You can check the documentation here.

Kinda old question, but now you can use Answers which is part of the Fabric suit (Crashlytics is part of Fabric as well):

Fabric can be found here. And further documentation here.

I tried the below lines and it works like charm. In try-catch block use the below lines in your catch block

@try {
// line of code here
}
@catch (NSException *exception) {
NSUncaughtExceptionHandler *handler = NSGetUncaughtExceptionHandler();
handler(exception);
}

as explained at http://support.crashlytics.com/knowledgebase/articles/222764-can-i-use-a-custom-exception-handler

[UPDATE]

Now in fabric's crashlytics we can use simple function [Crashlytics recordCustomExceptionName:reason:frameArray:] for sending handled exceptions

@try {
// line of code here
}
@catch (NSException *exception) {
    NSArray *stack = [exception callStackReturnAddresses];
    [[Crashlytics sharedInstance] recordCustomExceptionName: exception.name
                                                 reason: exception.reason
                                             frameArray: stack];
}

as explained at https://twittercommunity.com/t/crashlytics-ios-how-to-send-non-fatal-exceptions-without-app-crash/34592/32

For me the method .recordError() didn't helped, because it don't log the user information. Or i just didn't found where to watch it. Using recordCustomExceptionName fit to me. There is an example of implementation of the both ways:

func logMessage(_ message: String) {
    let userInfo = ["message" : message]
    let error = NSError(domain: "AppErrorDomain", code: 1, userInfo: userInfo)
    Crashlytics.sharedInstance().recordCustomExceptionName("API Error", reason: message, frameArray: [])
    Crashlytics.sharedInstance().recordError(error, withAdditionalUserInfo: userInfo)
}

In reference from Crashlytics documents.

try {
  myMethodThatThrows();
} catch (Exception e) {
  Crashlytics.logException(e);
  // handle your exception here!
}

https://docs.fabric.io/android/crashlytics/caught-exceptions.html?caught%20exceptions#caught-exceptions

As far as I know, if you dont protect your code correctly, your application will crash anyway. Crashlylytics, take this crashes and show them to you in a "readable" mode in the web application they have designed. If there is no crash, crashlytics will take anything. You can grab an exception in your code :

@try{
....
}
@catch(NSException ex){...}

in the critical parts, but you should always do that if you are afraid your application is going to crash or you find a potential error which can allow your application have a bad behavior and acting up. You can always force in your exception to send or track this error.

Hope it helps

Crashlytics is a crash tracking service, if you need to track custom messages choose other analytics service.

The trick is to use the following :

http://support.crashlytics.com/knowledgebase/articles/202805-logging-caught-exceptions

Just use this :

Crashlytics.logException(new Exception("my custom log for crashLytics !!!"));

I use this and I get my non-fatal crash in about 5 minutes !

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!