C# c未处理异常捕获程序
我们有一个VS2013解决方案,最初是VS2012解决方案,但这与包含多个项目无关。一些类库。一些MVC应用程序。一些WinForm应用程序。一些comandline应用程序。我们想要的是一个类,我们可以在所有项目中使用它来捕获未处理的异常并将它们记录到一个可配置的目标。特定的应用程序可以决定是否要记录到平面文件、事件日志或数据库。其想法是,在启动时,应用程序将调用一个静态方法,或实例化一个对象,然后任何未处理的异常都将被神奇地记录到应用程序的其余部分 虽然第一次尝试只是记录到一个平面文件,但我认为这会起作用:C# c未处理异常捕获程序,c#,logging,unhandled-exception,C#,Logging,Unhandled Exception,我们有一个VS2013解决方案,最初是VS2012解决方案,但这与包含多个项目无关。一些类库。一些MVC应用程序。一些WinForm应用程序。一些comandline应用程序。我们想要的是一个类,我们可以在所有项目中使用它来捕获未处理的异常并将它们记录到一个可配置的目标。特定的应用程序可以决定是否要记录到平面文件、事件日志或数据库。其想法是,在启动时,应用程序将调用一个静态方法,或实例化一个对象,然后任何未处理的异常都将被神奇地记录到应用程序的其余部分 虽然第一次尝试只是记录到一个平面文件,但我
public class InfLogger
{
public string LogFile { get; set; }
public InfLogger()
{
System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(ThreadExceptionHandler);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionsHandler);
LogFile = "C:\\Infinedi.log";
}
public void ThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs e)
{
try
{
if (File.Exists(LogFile))
{
File.AppendAllText(LogFile, e.Exception.Message + "\n");
}
else
{
File.WriteAllText(LogFile, e.Exception.Message + "\n");
}
}
catch (Exception ex)
{
}
}
public void UnhandledExceptionsHandler(object sender, UnhandledExceptionEventArgs e)
{
try
{
if (File.Exists(LogFile))
{
File.AppendAllText(LogFile, (e.ExceptionObject as Exception).Message + "\n");
}
else
{
File.WriteAllText(LogFile, (e.ExceptionObject as Exception).Message + "\n");
}
}
catch (Exception ex)
{
}
}
}
}
但是没有快乐
在我用来尝试和测试它的MVC应用程序中,我实例化了InfLogger对象,然后故意创建一个数组索引超出范围的未处理异常。但记录器不会触发任何一个事件处理程序:
有没有关于如何实现这一目标的建议
我做了一些研究,这确实适用于控制台应用程序和winforms。但不是MVC。六羟甲基三聚氰胺六甲醚。。。不知道为什么。但我发现了一个新问题。这里是使用代码的地方
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
InfLogger logger = new InfLogger();
int[] a = { 0, 1, 2 };
int y = a[6];
}
}
}
记录器确实捕获了异常。但是执行只是挂起:int y=a[6]。它只是不断执行该行,抛出异常,一遍又一遍地捕获未处理的异常。因此,现在我需要知道如何让执行实际移动到抛出异常的行之外。我调用Program.cs文件中的方法来设置未处理的异常方法。下面是Application.SetUnhandledExceptionMode方法的完整说明
在实例化类的地方,您的ref是否可能超出范围,并获取gc'd?InfoLogger类是否与引用它的项目位于同一程序集中?因此不会引发任何事件。当抛出异常时会发生什么?当WinForm apps用户InfoLogger时这是否有效?在WCF项目中,显然需要使用[不同的方法][1]。[1] :我不希望每个应用程序都必须设置其事件处理程序。我想要一个每个应用程序都可以使用的类。
// ....
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
SetUnhandledExceptionHandler();
// ...
#region -- SetUnhandledExceptionsHandler() Method --
/// <summary>
/// Causes all unhandled exceptions to be handled by the application.
/// This will not prevent the application from being forced to close, but
/// does give a chance to log the error.
/// </summary>
public static void SetUnhandledExceptionHandler()
{
// Add an exception handler for all UI thread exceptions
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
// Set the unhandled exception mode to force all Windows Forms errors to
// Go through this handler
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
#endregion