Joh*_*ski 4 c# asp.net error-handling error-logging enterprise-library
下面的代码使用Microsoft的企业库日志记录应用程序块。您如何使其“更好”?
using Microsoft.Practices.EnterpriseLibrary.Logging;
class Program
{
static void Main(string[] args)
{
try
{
// Trying to write some data to the DB
...
}
catch (Exception ex)
{
LogHelper.LogException(ex, "Trying to write to the DB");
}
}
}
public class LogHelper
{
public static void LogException(Exception ex, string exceptionType)
{
try
{
// Simplified version, only logging the message
Logger.Write(exceptionType);
}
catch
{
// What do you do here???
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用通常会针对错误执行两项操作。第一种是将其写入本地日志文件(即使它也使用某种类型的数据库日志记录)。然后,它将错误的电子邮件发送到为支持而设置的通讯组列表。
因此,如果数据库日志写入失败,则错误仍然存在于本地日志文件中,并且还会通过电子邮件发送。
如果电子邮件失败,该错误仍会记录下来,以便您以后可以解决问题。
拥有更多的容错能力仅对极端关键任务的应用程序IMHO值得。