What is the difference between Console.WriteLine()
and Trace.WriteLine()
?
问题:
回答1:
Look at these from the "Debugging" perspective.
- We started debugging using
Console.WriteLine()
- Later we got to know it might not be good to print debugging data on console always. We might not even have a console. Then we started using
Debug.WriteLine()
, which prints my debug information on Visual Studio output window. - Then we got to know that we shouldn't print all debug information in release mode, so we should use
Trace.WriteLine()
in release mode. In debug mode we can see outputs from bothDebug.WriteLine()
andTrace.WriteLine()
. - Here's a very good reference: Usage of Trace and Debug
You can use the Trace and the Debug classes separately or together in the same application. In a Debug Solution Configuration project, both Trace and Debug output are active. The project generates output from both of these classes to all Listener objects. However, a Release Solution Configuration project only generates output from a Trace class. The Release Solution Configuration project ignores any Debug class method invocations."
Here are some relevant items that you might find useful:
- Where to look for trace logs ?
- How to set Debug or Release Configurations ?
- Understanding build configurations.
回答2:
From MSDN website:
Console.WriteLine()
writes the specified data, followed by the current line terminator, to the standard output stream. Meanwhile, Trace.WriteLine()
writes information about the trace to the trace listeners in the Listeners collection