views:

260

answers:

6

I'm looking for an library/tool for .NET that logs almost everything that happens in my C# application (Windows Form).

The problem is that I'm delivering an application to a client (Windows XP) and after doing some task, that classic Microsoft error window appears:

"ApplicationName has encountered a problem and needs to close. We are sorry for the inconvenience"

I'm currently handling my application exceptions, but this is something external and I can't get anything from that error, so I would like any automated library that helps me with that.

It would work if it logs each line of code executed, orr just log what line was executing before that error appeared, or something that could give me more info about that error.

P.S: It's a multithreaded application, and have to Timer controls (one for watching a folder every 5secs, and another for watching thread list...). I'm using Windows 7 here and everything seems to work ok.

+4  A: 

I don't think you're going to find a logging tool that you just plug in and it logs every single thing that happens. You're going to have to plug into the hooks that any logging framework out there has. Here is a popular, and free, one.

Jaxidian
@Jaxidian: Thanks for your reply. I'll test log4net and will let you know ;)
tsocks
A: 

I've used log4net in the past. You can just wire it up so anytime an uncaught exception occurs, it is logged to a file, or written to the database.

Shawn Simon
A: 

Try to log your exception in window event viewer with different entry level. Check sample code from MSDN http://support.microsoft.com/kb/307024

Wonde
beat me to it :p
msarchet
+2  A: 

Why not log in the windows event viewer, that's what it's made for. From a simple config setting you can toggle it to include all levels under, for example, 4- Verbose, 3- Information, 2-warning, 1-Error, you just log the message as one of these 4 types. You can also combine this with a boolean traceswitch for debug tracing, but it's not necessary.

The windows event viewer also offers you summaries, can be connected to remotely, and sysadmins are comfortable in working with it.

using System;

using System.Diagnostics;

class MySample{

public static void Main(){

    // Create the source, if it does not already exist.
    if(!EventLog.SourceExists("MySource")){
        EventLog.CreateEventSource("MySource", "MyNewLog");
        Console.WriteLine("CreatingEventSource");
    }

    // Create an EventLog instance and assign its source.
    EventLog myLog = new EventLog();
    myLog.Source = "MySource";

    // Write an informational entry to the event log.    
    myLog.WriteEntry("Writing to event log.");

}

}

Sounds like what you need is to put a breakpoint and step through your code, if it's threaded, just open the thread windows in visual studio while stepping through the code. Temporarily put longer delays on your timers if to many threads make it difficult to debug.

GenEric35
Based on the phrasing of the question, I'm not sure using the windows event viewer is ideal for the level of verbosity that he is going to log to. There are limits to the sizes of the windows event log, and depending on how it's configured, hitting those limits can cause some major problems. Also, since this is running on client computers, exporting and obtaining a copy of event lots is not very ideal. However, with log4net, saving it to the event log IS an option, just not one I would recommend.
Jaxidian
Certainly a lot of logging needs to be done during development, or even debugging in prodocution, but that is temporary and you often clear your event log to start fresh, admins know how to configure it and don't need to call you for it. In production it shouldnt be set to verbose, I don't see the point of showing every method name in the log... infact managed code, in production, should be obfuscated and the method names hidden.Also you could consider this ETW in .Net4.0, also used for logging and new. http://msdn.microsoft.com/en-us/library/dd264810(v=VS.100).aspx
GenEric35
A: 

The closest you will get regarding your requirement ("logs almost everything that happens in my C# application") is to use a logging framework with AOP support and automated exception handling. Aspect oriented programming will allow you to instrument your methods and trace method execution. Together with a logging tool such as SmartInspect (disclaimer: I'm one of the developers behind SmartInspect) you can monitor and analyze the method execution and view exceptions in context (see screenshot below).

To log your application data and events, you will have to instrument your code manually, there's really no way around it. If you have a large application, manually instrumenting all of your code at once won't work (it's just too time consuming), so I generally recommend starting with your most critical routines and modules first.

alt text

Dennis G.
A: 

Have you tried ETW? ETW is the fastest and the least overhead logging system in windows. ETW is built into Kernel. MS uses ETW for every application they build ,from Windows to VS.NET.

This is not a automated logging tool. But will give you stack-trace of every call in your application without you having to modifying your code. If you need add custom logging messages then you would have write code for this.

Here are some of the links for ETW and I also have few posts on ETW in managed code

Naveen