Matthew Knudsen
Matthew Knudsen

Reputation: 213

Catching exceptions in console application C#

I have a console application written in C#. This application runs as automation to test a web service. Flow:

  1. Log in to network share (impersonate user)
  2. copy mp3 file to local disc
  3. convert mp3 to wav
  4. downsample
  5. trim wave
  6. extract some useful data from wav
  7. send http request
  8. delete local files
  9. write out some stuff to tsv

The application will run great for several hours (usually takes about 24 hours to complete the test). but every once and a while I will get this message: "The application has stopped working. I have been running this is VS 2012 in debug mode so, I can see what line throws any error. problem is, that I have not been able to catch the line (or method) that is throwing the error. I originally thought that the Domain controller was causing this issue due to power settings.

How can I capture exactly what error is bubbling its way up the stack?

Upvotes: 0

Views: 11858

Answers (5)

Nick Freeman
Nick Freeman

Reputation: 1411

It looks like the exception code you are getting happens when you try to use something that is already been garbage collected. Are you using anything after it is disposed?

Knowledge Base Article for 0xc0000005

Upvotes: 0

Luis Quijada
Luis Quijada

Reputation: 2405

I recommend you using a logging framework.

I use log4net in almost all applications. Its very simple to use and configure.

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

try
{
    // do whatever
}
catch(Exception ex)
{
     // Log an error with an exception
     log.Error("Exception thrown", ex);
}

By using these kind of libraries you can get your log data output to file, database or even written to the windows event-viewer for instance.

Upvotes: 0

Xaqron
Xaqron

Reputation: 30837

try
{
   // your code
}
catch (Exception e)
{
   System.IO.File.WriteAllText(@"Z:\err.txt", e.ToString());
}

Note that access to windows drives are denied for non administrators so replace Z: with your choice.

Upvotes: 0

mutex
mutex

Reputation: 7728

Does all that run in a loop of some kind? Or on a timer?

Perhaps put a try-catch around the body of the loop or the method that runs all your code, add a logging framework of your choice (log4net or nlog seem good) and then in the catch log the exception. Most logging frameworks allow you to include the exception and will include stacktrace, etc.

Putting debug logging throughout the process can also help to narrow down where it's happening.

Upvotes: 2

RiceRiceBaby
RiceRiceBaby

Reputation: 1596

You can go to the Event Viewer on the operating system the console application is running on and then click on "Application". Event viewer logs and displays all exceptions thrown on any application running on the operating system.

Upvotes: 2

Related Questions