【问题标题】:How can I log errors automatically using Serilog?如何使用 Serilog 自动记录错误?
【发布时间】:2017-05-24 21:19:45
【问题描述】:

我正在构建一个控制台应用程序,它基本上只是调用数据库的存储过程。这工作正常,但现在我遇到了这个小问题 - 错误处理。

我正在使用 Serilog 将任何错误记录到文本文件中,但我遇到的问题是我必须在任何地方都执行 try-catch。当然,对于一个小软件来说,使用和放置 try-catch 并没有什么痛苦,但是在构建大型软件时,我猜你有时会忘记放置错误处理代码?所以我想知道是否有一种方法可以让 Serilog 在默认情况下记录并打印出错误??

【问题讨论】:

标签: c# error-handling asp.net-core serilog


【解决方案1】:

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
  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多