how to use trace?

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

this is what I want to do.

i have an input box and a submit button. the input gets processed by my
program and it returns the results. depending on the input, not all the
functions in my program are called, some might be called multiple
times, the order of functions called might be different each time. I
would like to log all this, what function actually got called...

this is my plan
create a global string named TRACE
in each function add its name to trace

string processInput1(string input)
{
//process input
....

TRACE = TRACE + "processInput1";
return input
}


but this is method is clumsy. is there a builtin function in dotNet?
please show me a good example.

Thanks in Advance
Aaron
 
Your wish is granted.

The good folks at Microsoft predicted your need eons ago.

From within your ASPX code;

Trace.Write ( whatever you want to be logged );

Place this anywhere in your code, as often as you require.

In your Web.config, look for the following section;

<trace
enabled="true"
requestLimit="100"
pageOutput="false"
traceMode="SortByTime"
localOnly="false"
/>

Make sure to set enabled to "true"; I also like to set localOnly to false
and to increase the requestLimit.

Run your page, do your testing, and then CTRL+N to open a new browser
window. Change the Url to;

http://yoursite/trace.axd
or
http://yoursite/yourvirtualdir/trace.axd
if you're using virtual directories

You'll be surprised at what you find.

/// M
 
Regarding the above, I'm assuming that you're using ASP.NET, since you've
included that group in your post. If not, your approach may be slightly
different.

Also, do not include the System.Diagnostics namespace. You want to use the
Trace functionality built directly into the HttpContext.Current class,
rather than a new orphan Trace object. If you include System.Diagnostics,
your trace output will not appear in Trace.axd; rather you will have to
specify a listener.

/// M
 
Ah, in that case you do want to use the Trace in System.Diagnostics, and
write yourself a custom listener that outputs to your database.

I recommend Dan Appleman's eBook "Tracing and Logging with .NET"; a whopping
US$10 through Amazon. Or direct from Desaware's site;

http://www.desaware.com/products/books/net/tracelogging/index.aspx

Or do some Googling on "System.Diagnostics.Trace" or
"System.Diagnostics.Trace.Listeners.Add"; you should be able to assemble
similar best practices for using Trace, and examples for how to write your
own Listener.
 
Correct; I put a Trace.Write () at the top of my methods when I need to
track the call path.
 
Back
Top