Dan*_*nny 5 c# error-handling serilog asp.net-core
我正在构建一个控制台应用程序,该应用程序基本上只是调用数据库的存储过程。这可以正常工作,但是现在我遇到了这个小问题-错误处理。
我正在使用Serilog记录我在文本文件中遇到的所有错误,但是与此相关的问题是,我必须在所有地方都进行尝试捕获。当然,对于一个小的软件来说,使用并放置try-catch并不麻烦,但是在构建大型软件时,我想您有时可能会忘记输入错误处理代码?所以我想知道是否有一种方法可以告诉Serilog,只要有默认设置,就可以记录并打印错误?
任何想法/解决方案将不胜感激。
谢谢
小智 0
https://stackify.com/catch-unhandled-exceptions-csharp/
static void Main(string[] args)
{
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//do something with the file contents
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// Log the exception, display it, etc
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// Log the exception, display it, etc
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2608 次 |
| 最近记录: |