i'm trying to do globalexceptionhandling, i've already tried 3 methods to do this but nothing caught an exception thrown somewhere in the program
static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(Catch);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(Catch);
try
{
Application.Instance.Start();
}
catch (Exception ex)
{
StackTrace st = new StackTrace(ex, true);
StackFrame[] frames = st.GetFrames();
List<string> errorList = new List<string>();
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var frame in frames)
{
errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
}
SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());
SyslogUdpSender sender = new SyslogUdpSender("localhost", 514);
sender.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());
}
}
static void Catch(object sender, System.Threading.ThreadExceptionEventArgs e)
{
StackTrace st = new StackTrace(e.Exception, true);
StackFrame[] frames = st.GetFrames();
List<string> errorList = new List<string>();
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var frame in frames)
{
errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
}
SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());
SyslogUdpSender send = new SyslogUdpSender("localhost", 514);
send.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());
}
static void Catch(object sender, UnhandledExceptionEventArgs e)
{
StackTrace st = new StackTrace((Exception)e.ExceptionObject, true);
StackFrame[] frames = st.GetFrames();
List<string> errorList = new List<string>();
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var frame in frames)
{
errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
}
SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());
SyslogUdpSender send = new SyslogUdpSender("localhost", 514);
send.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());
}
this is what i've already tried, nothing caught the exception.
and i'm not using the standard application.run method, i'm using a singleton class to start from, where for every view(form) i have a presenter gets created in which the view gets created
does anyone know how to do globalexception handling with this setup?
also, sorry for my bad english
best regards
EDIT: MVCE
namespace Application
{
static class Program
{
static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(Catch);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(Catch);
try
{
Application.Instance.Start();
}
catch (Exception ex)
{
//do some catching
}
}
static void Catch(object sender, System.Threading.ThreadExceptionEventArgs e)
{
//do some catching
}
static void Catch(object sender, UnhandledExceptionEventArgs e)
{
//do some catching
}
}
public class Application
{
private static Application _instance;
private Application()
{
}
public static Application Instance
{
get
{
return _instance ?? (_instance = new Application());
}
}
internal void Start()
{
StartOverviewWindow();
}
private void StartOverviewWindow()
{
throw new Exception();
}
}
}
As a quick answer (because I can't find duplicates for all of those), to handle
you have to handle following events:
For difference between
Application.ThreadException
andAppDomain.CurrentDomain.UnhandledException
see this.