[C#] Een exception loggen

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Hyperz
  • Registratie: Augustus 2009
  • Laatst online: 09-07 02:45
Hoi.

Ik heb een klein vraagje i.v.m. het loggen/dumpen van een exception naar en text bestand. In een applicatie heb ik momenteel het volgende gedaan om unhandled exceptions op te vangen:

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace Test
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            Type t = e.Exception.GetType();
            Object ex = Convert.ChangeType(e.Exception, t);
            String msg = String.Format(
                "Exception: \r\n{0}\r\n\r\nThread ID: {1}",
                t.InvokeMember("ToString", System.Reflection.BindingFlags.InvokeMethod, null, ex, new object[0]),
                e.Dispatcher.Thread.ManagedThreadId
            );

            MessageBox.Show(msg, t.Name, MessageBoxButton.OK, MessageBoxImage.Error);
            this.Shutdown(-1);
            e.Handled = true;
        }
    }
}


Het gaat dus om een WPF applicatie. Ik weet dat de ToString() method van een exception een string geeft met het type en stacktrace etc. Het probleem is dat sommige exceptions extra properties hebben die extra info hebben. Dus Ik dacht dat als ik die exception convert naar de originele exception type end daarna ToString() aanroep dat die string die extra info (de properties dus) zou hebben. Blijkbaar niet dus ;( .

Nu vraag ik me af hoe ik het best een string kan aanmaken die _alle_ info van de originele exception heeft (somige IOExceptions hebben bv ook een FileName property etc)? Hopelijk ben ik duidelijk genoeg. Alvast bedankt :) .

Asus P8P67 EVO | i5 2500k (4.8 GHz) | Sapphire HD 7970 Vapor-X GHz Ed. | 8 GB DDR3 1600 | 1 TB HDD


Acties:
  • 0 Henk 'm!

  • Alex)
  • Registratie: Juni 2003
  • Laatst online: 21-08 11:20
Als je alles wilt loggen, zou je kunnen overwegen het naar XML te serializen. Anders zou je ook nog d.m.v. Reflection door de Exception heen kunnen gaan en zo alle properties ophalen.

Misschien is de Logging Block van Enterprise Library ook nog wel bruikbaar?

We are shaping the future


Acties:
  • 0 Henk 'm!

  • Compuhair
  • Registratie: September 2009
  • Laatst online: 10:57
ik zou eens naar ELMAH kijken:

* Logging of nearly all unhandled exceptions.
* A web page to remotely view the entire log of recoded exceptions.
* A web page to remotely view the full details of any one logged exception.
* In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
* An e-mail notification of each error at the time it occurs.
* An RSS feed of the last 15 errors from the log.