Search code examples
.netexceptionunhandled-exceptionapplicationdomain

Application global Error Logger


While working on a vb.net application, in which exceptions were handled poorly. It was a big pain. So an Idea flashed , that if it is possible to genearte a library or service or some sort of exe that will log all error of an application without writing a single line of code / minimal code at some global location in host application. I know it sounds bit crazy but Windows Event Logger does the same. So i thought for the same. Let me make me more clear. Say I have an application abc.exe, with not a single try catch. I am facing lots of bug while using that app. Now instead of digging my head into the application source, I want to code another app that will run and log all unhandled exceptions of that app, including managed and com both.

So gurus are requested to help me out. I know ApplicationDomain is useful. I came across a guy name Rick or something similar over MSDN blog but i did'nt grab any more. So some easy source or part of code will be helpful. Please add some sample code with your answers.

It would be a great help.

SUMMARY

I want a application that will inject itself into any application , where the assembly of Injector is placed. That is If i have an application D:\xyz\myapp\abc.exe , if say i had the application as exe then if place it inside myapp folder then after the execution of abc.exe , universal my error logger application will start working and inject itself to the current application domain. Also tell me if it will be made multi threaded , if i create it as a service.


Solution

  • This MSDN page How to: Log Exceptions in Visual Basic exactly describes what you need. In summary, it describes:

    • How to use the Visual Basic My.Application.Log to log exceptions using .NET's built in trace listeners.
    • How to register the UnhandledException Forms Application event in VB applications.
    • How to register Visual Basic's FileLogTraceListener to log to the file "[User]\Application Data\[CompanyName]\[ProductName]\[ProductVersion][ApplicationName].log".

    Here's an example of an application file configuration that registers the VB FileLogTraceListener:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.diagnostics>
            <sources>
                <source name="DefaultSource" switchName="DefaultSwitch">
                    <listeners>
                        <add name="FileLog"/>
                    </listeners>
                </source>
            </sources>
            <switches>
                <add name="DefaultSwitch" value="Information" />
            </switches>
            <sharedListeners>
                <add name="FileLog"
                    type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
                    initializeData="FileLogWriter" />
            </sharedListeners>
        </system.diagnostics>
    </configuration>