Solved

TraceListener vs Try-Catch?

Posted on 2008-10-09
5
432 Views
Last Modified: 2008-10-13
Q. What's the difference between using TraceListeners and placeing a Try-Catch on every method???

Q. Looks like I would need a Try-Catch anyway in order to use the Trace.WriteLine method???

I've been using the following code on every method to catch errors. It calls a class implementing a StreamWriter to write both method() name and exception message to a c:\log.txt......  The log.tx is also encrypted to keep prying eyes from reading the detailed exceptions (ie... methods, line numbers, etc...) Encryption can also be disabled for my eyes only or I can decrypt a log sent to me from a client.

Try
{
      //code....
}
catch (Exception ex)
{      
      EventLogWriter.Write(" Form1_Load() " + ex.ToString(), "Error");
}

0
Comment
Question by:pointeman
5 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 83 total points
ID: 22678406
I always thought that a traceListener is used for creating a log-file where you store the errors, but also some event to see if they happen, how many times they happen, ...

A try-catch-event will only catch an error

You can use the Trace.WriteLine("Exiting from Main()") without a try-catch, but if there is something wrong with the trace (for example if you're tracing to a file) and the file is removed, edited by someone else, ...)
   
0
 
LVL 5

Assisted Solution

by:Ignacio Soler Garcia
Ignacio Soler Garcia earned 83 total points
ID: 22678459
Anyway this way of managing the errors is not the best one, I know because I used it before. Let me tell you what I've learned:

Its better to let the Exception to go up to the initial caller and log there the exception (you have the stack trace to do that). This way you can handle the exception and do whatever is expected to do with errors (usually inform the error, maybe mail it and end the application).

What will you do if you had a NullReferenceException inside that FormLoad event?
0
 

Author Comment

by:pointeman
ID: 22678680
Okay, I've found many TraceListener examples on the Internet. Some use a custom class.cs while other use the app.config file.
Q. Which is better, the class or .config file?
Q. Does the TraceListener automatically log messages or do have to continually use a Trace.WriteLine?
It would be nice if the TraceListener class could be initialized when the application starts...
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 84 total points
ID: 22680828
you have to manually use trace.writeline
you can construct and initialize your own tracelistener class at program Main function, and put it in some class object as a static member.
Also, notice using try/catch comsumes lots of time, so you shoud use it just when there is a true error and you done have other alternative.
0
 

Author Comment

by:pointeman
ID: 22682809
I have a StackTrace setup in a web site global.asax that reports any errors occuring via email.
Too bad Window Apps don't have this ability.
I was hoping the Win App Tracing/Debugging offered something similar.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now