ios 将日志发送到 Crashlytics,而不会导致应用程序崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24950777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:33:19  来源:igfitidea点击:

Send a log to Crashlytics without an app crash

ioscrashlytics

提问by Hyman

How can I get Crashlytics to receive a log without my app crashing? I have the following code:

如何让 Crashlytics 在不崩溃的情况下接收日志?我有以下代码:

if(!context.managedObjectContext save:&error) {
    CLS_LOG(@"%@",error.description)
}

When an error occurs, I want the Crashlytics server to receive the error but the app should continue running.

发生错误时,我希望 Crashlytics 服务器收到错误,但应用程序应继续运行。

I do not need the log right away. I would be happy to get the log on the next restart. I just do not want to have to trigger a crash in my app to receive the log.

我现在不需要日志。我很乐意在下次重新启动时获取日志。我只是不想在我的应用程序中触发崩溃来接收日志。

Is this possible?

这可能吗?

回答by Tiago Almeida

With the new update from crashlytics you can now use:

通过 crashlytics 的新更新,您现在可以使用:

[[Crashlytics sharedInstance] recordError:error];

And in Swift:

在 Swift 中:

Crashlytics.sharedInstance().recordError(error)

You can check the documentation here.

您可以在此处查看文档。

回答by Rui Peres

Kinda old question, but now you can use Answerswhich is part of the Fabricsuit (Crashlyticsis part of Fabricas well):

有点老问题,但现在您可以使用Answers哪个是Fabric套装Crashlytics的一部分(也是其中的一部分Fabric):

enter image description here

在此处输入图片说明

Fabriccan be found here. And further documentation here.

面料可以在这里找到。和进一步的文档here

回答by Aanabidden

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

我尝试了以下几行,它就像魅力一样。在 try-catch 块中,在 catch 块中使用以下几行

@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

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

现在在 fabric 的 crashlytics 中,我们可以使用简单的函数[Crashlytics recordCustomExceptionName:reason:frameArray:]来发送已处理的异常

@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

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

回答by Nik Kov

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 recordCustomExceptionNamefit to me. There is an example of implementation of the both ways:

对我来说,该方法.recordError()没有帮助,因为它不记录用户信息。或者我只是没有找到在哪里观看它。使用recordCustomExceptionName适合我。有一个实现这两种方式的例子:

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)
}

回答by wildcat12

Swift 5, Crashlytics SDK 4.0.0-beta.6:

Swift 5,Crashlytics SDK 4.0.0-beta.6:

let exceptionModel = ExceptionModel(name: "exception title", reason: "details")
Crashlytics.crashlytics().record(exceptionModel: exceptionModel)

...similar for NSError, with whatever you want to see in the Crashlytics dashboard.

...类似于 NSError,您可以在 Crashlytics 仪表板中看到任何内容。

let error = NSError(domain: "error title", code: 0, userInfo: ["message":"some details"])
Crashlytics.crashlytics().record(error: error)

回答by Muhammad Aamir Ali

In reference from Crashlytics documents.

参考 Crashlytics 文档。

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

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

回答by avdyushin

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

Crashlytics 是一种崩溃跟踪服务,如果您需要跟踪自定义消息,请选择其他分析服务。

回答by Tobliug

The trick is to use the following :

诀窍是使用以下内容:

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

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 !

我使用它,我在大约 5 分钟内得到了非致命的崩溃!

回答by AlfuryDB

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 :

据我所知,如果你没有正确保护你的代码,你的应用程序无论如何都会崩溃。Crashlylytics,处理这些崩溃并在他们设计的 Web 应用程序中以“可读”模式向您展示它们。如果没有崩溃,crashlytics 将采取任何措施。您可以在代码中获取异常:

@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

希望能帮助到你