Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trace logs location, where to view them

Tags:

Where do you see Trace.Write(""); logs while developing an MVC or WCF app? What is the correct place to look at?

like image 608
DarthVader Avatar asked Aug 13 '14 12:08

DarthVader


People also ask

How can log files be traced?

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.

Which method is used to start logging trace files?

To create trace logs, call startMethodTracing() where you want the system to start logging tracing data.

Where does trace WriteLine write to?

WriteLine(String) Writes a message to the trace listeners in the Listeners collection.


1 Answers

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:

debug output

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.

like image 111
schellack Avatar answered Sep 17 '22 06:09

schellack