我已经能够找到无数关于如何在 Spring MVC 或 Spring REST 中捕获未处理的异常的教程,但我想知道的是如何在不使用 Spring Web 框架的情况下捕获未处理的异常。
我正在编写一个没有 Web 组件的应用程序,并且我不打算仅为异常处理而导入 Spring Web。
当@Service
抛出未处理的异常时,我需要捕获它,以便我可以将其正确记录到Raygun。
例如,考虑一个故意抛出未捕获异常的服务中的这个方法:
@Scheduled(fixedDelay = 100)
public void doSomething() {
throw new RuntimeException("Uh oh!");
}
Run Code Online (Sandbox Code Playgroud)
它的输出将是:
2017-08-16 00:19:40.202 ERROR 91168 --- [pool-1-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.
java.lang.RuntimeException: Uh oh!
at com.mitchtalmadge.example.ExampleService.doSomething(ClassSyncService.java:48) ~[classes/:na]
at com.mitchtalmadge.example.ExampleService$$FastClassBySpringCGLIB$$1dd464d8.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669)
...
Run Code Online (Sandbox Code Playgroud)
我怎么抓住它?
没有简单的方法可以做到这一点吗?
我正在使用 MvvmCross 并尝试使用一些方法在我的报告工具中获取 MvxTrace。在这种情况下,我使用的是 Raygun。Raygun 为我提供了在我想抛出的错误消息中包含附加消息的选项,我认为我必须使用它来实现这一点。基本上我想在代码中做这样的事情:
var client = new RaygunClient();
var tags = new List<string> { "myTag" };
var customData = new Dictionary<int, string>() { {1, "**MVXTrace stuff here**"} };
client.Send(exception, tags, customData);
Run Code Online (Sandbox Code Playgroud)
我怎么能把它挂起来?当我查看 Trace 设置时,我感到很困惑。我假设我需要对我用来注入的 DebugTrace 文件做一些事情。现在它看起来像这样:
public class DebugTrace : IMvxTrace
{
public void Trace(MvxTraceLevel level, string tag, Func<string> message)
{
Debug.WriteLine(tag + ":" + level + ":" + message());
}
public void Trace(MvxTraceLevel level, string tag, string message)
{
Debug.WriteLine(tag + ":" + level + ":" …
Run Code Online (Sandbox Code Playgroud) 我有一个向Raygun报告崩溃的Xamarin.Android应用程序。从Release版本报告给Raygun的堆栈跟踪不包括行号。如果我为Release构建配置提供与.csproj文件中的Debug配置相同的设置:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
...
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
那么我仍然看不到发送给Raygun的堆栈跟踪中的行号。但是,如果我在运行带有附加的Visual Studio调试器的应用程序时,则发送到Raygun的堆栈跟踪信息确实包含行号。请注意,无论如何,所有堆栈跟踪都包括类和方法的名称。这个问题只与行号有关。
如果引发异常时附加了调试器,为什么堆栈跟踪仅包含行号?更重要的是,如何在没有附加调试器的情况下,在Release版本的报告堆栈跟踪中获取行号?
我正在尝试从程序中创建的go例程中捕获崩溃/紧急情况,以便将其发送到崩溃错误报告服务器(例如Sentry / Raygun)
例如,
func main() {
go func() {
// Get this panic
panic("Go routine panic")
}()
}
Run Code Online (Sandbox Code Playgroud)
答案表明,一个goroutine无法从另一个goroutine的恐慌中恢复。
惯用的方式是什么?