surenv2003
surenv2003

Reputation: 119

Trace log in .Net. Why?

When we can create simple text log file(.log) using textwriters So why there is Trace and debug classess. I mean what is the actual need of Trace and loging.

When to use them with a simple and good example!

I tried them in my code the default output is in output window. I tried:

Trace.Listeners.Add(new TextWriterTraceListener("TextWriterOutput.log", "myListener")); Trace.TraceInformation("Test message."); Trace.WriteLine("suren","General - Error Tracing Enabled");

but this is not writing in the file specified!!

Thanks Suren

Upvotes: 1

Views: 7133

Answers (1)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64933

Debug is a way to log if your project compilation mode is in "Debug". Proper compiler in "Release" mode, C# or VB, or any other, won't compile/execute any logging with Debug class.

Trace would do even in Debug or Release compilation mode.

Also, both are a simpler way to have an universal logger, which can output to any kind of stream, because you've trace listeners that can handle in a customized way how to write log's text wherever you want:

Debug/Trace have a default listener implementation that ouputs texts to Visual Studio's output window. But as I said, you can output anywhere with your custom listeners.

You'd like to use them because no one wants to reinvent wheels and if the simpler out-of-the-box .NET solution for basic logging fits your needs, and anyone knows about it, why you wouldn't use it? :)

Upvotes: 4

Related Questions