Where do you see Trace.Write("");
logs while developing an MVC or WCF app? What is the correct place to look at?
An event trace log (. etl) file, also known as a trace log, stores the trace messages generated during one or more trace sessions. The system first stores the trace messages that trace providers generate in trace session buffers, and then delivers them directly to a trace consumer or writes them to a trace log.
To create trace logs, call startMethodTracing() where you want the system to start logging tracing data.
WriteLine(String) Writes a message to the trace listeners in the Listeners collection.
When using the System.Diagnostics.Trace class, the Write method writes its trace output "to the trace listeners in the Listeners collection." The Trace.Listeners property by default only contains an instance of the DefaultTraceListener, which outputs messages to the debugger output window. To view those trace messages, you have to enable debugging, of course.
So if you debug your WCF service or ASP.NET app in Visual Studio, you will see the trace output in the VS Output pane. For example, this code:
System.Diagnostics.Trace.WriteLine("GetData method was called.");
...causes this output to appear:
If you don't want to run a debugger to see the trace output, you can remove the DefaultTraceListener and replace it with another, e.g., a TextWriterTraceListener that will output your trace to a file. This can be done by creating a web.config file with the following content (or just add the system.diagnostics section to your pre-existing web.config):
<configuration> <system.diagnostics> <trace autoflush="true" indentsize="4"> <listeners> <remove name="Default" /> <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\myListener.log" /> </listeners> </trace> </system.diagnostics> </configuration>
After that (assuming you are running in a mode that has access to write to the output location), your traces will be output to the specified file.
If you want to write your traces to the Event Log instead of to a file, you can do that too with an EventLogTraceListener:
<configuration> <system.diagnostics> <trace autoflush="false" indentsize="4"> <listeners> <add name="myListener" type="System.Diagnostics.EventLogTraceListener" initializeData="TraceListenerLog" /> </listeners> </trace> </system.diagnostics> </configuration>
Just take care to ensure that your app is running under an account context with access to write to the Event Log.
There is a lot more you can do with tracing (such as have it output to the ASP.NET page itself. You'll find a walkthrough with more examples here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With