<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C# Frequently Asked Questions : Debugger/Debugging</title><link>http://blogs.msdn.com/csharpfaq/archive/tags/Debugger_2F00_Debugging/default.aspx</link><description>Tags: Debugger/Debugging</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How do I send out simple debug messages to help with my debugging?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/10/09/How-do-I-send-out-simple-debug-messages-to-help-with-my-debugging_3F00_.aspx</link><pubDate>Tue, 10 Oct 2006 02:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:810526</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/810526.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=810526</wfw:commentRss><description>&lt;P&gt;Visual Studio offers tons of useful debugging features and allows you to step through your code line-by-line. However, there are times when you don’t want to step through your application, but want to make it output simple text strings with variable values, etc.&lt;/P&gt;
&lt;P&gt;Enter the System.Diagnostics.Debug class and its Write* methods. By using the Debug class, you can output messages similarly to the way the Win32 API function OutputDebugString. However, the beauty of the Debug class is that when you build your application using the default Release configuration in Visual Studio, no code lines are generated for your Debug.Write* class. This means there’s no performance penalty for using the Debug class in release code.&lt;/P&gt;
&lt;P&gt;To use the Debug class, simply add the “&lt;CODE&gt;using System.Diagnostics;&lt;/CODE&gt;” statement to your C# code file, and call Debug.Write:&lt;/P&gt;&lt;PRE&gt;Debug.Write("Hello, Debugger!");&lt;/PRE&gt;
&lt;P&gt;In addition to Write, you have the possibility to call WriteIf, WriteLine and WriteLineIf. For example:&lt;/P&gt;&lt;PRE&gt;bool @this = true;
bool that = false;
Debug.WriteLineIf(@this || that, "A conditional Hello!");&lt;/PRE&gt;
&lt;P&gt;When you are debugging your application under the Visual Studio debugger, all the messages that are sent out by your Write method calls end up in the Output window (View / Output menu command or Ctrl+W,O on the keyboard). However, if you are running your application outside the debugger (say, by starting it from Windows Explorer), you can still view the messages using tools like &lt;A href="http://www.sysinternals.com/Utilities/DebugView.html" mce_href="http://www.sysinternals.com/Utilities/DebugView.html"&gt;DebugView from Sysinternals&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Remember, if you build your application using the default Release configuration, even DebugView won’t show your messages because the Debug.Write* calls are eliminated altogether. You can also control code generation by defining the DEBUG conditional directive.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Tip:&lt;/STRONG&gt; The .NET debugging/tracing architecture also allows you to redirect debugging messages to different destinations, such as text files. See the help topic “Trace Listeners” for more information.&lt;/P&gt;
&lt;P&gt;[author: &lt;A href="http://www.saunalahti.fi/janij/" mce_href="http://www.saunalahti.fi/janij/"&gt;Jani Järvinen&lt;/A&gt;, C# MVP]&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=810526" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Debugger_2F00_Debugging/default.aspx">Debugger/Debugging</category></item><item><title>How can I easily log a message to a file for debugging purposes?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/27/562555.aspx</link><pubDate>Tue, 28 Mar 2006 04:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:562555</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/562555.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=562555</wfw:commentRss><description>&lt;P&gt;Often, you need a way to monitor your applications once they are running on the server or even at the customer site -- away from your Visual Studio debugger. In those situations, it is often helpful to have a simple routine that you can use to log messages to a text file for later analysis.&lt;/P&gt;
&lt;P&gt;Here’s a simple routine that has helped me a lot for example when writing server applications without an user interface:&lt;/P&gt;&lt;PRE&gt;using System.IO;
        
public string GetTempPath()
{
    string path = System.Environment.GetEnvironmentVariable("TEMP");
    if (!path.EndsWith("\\")) path += "\\";
    return path;
}

public void LogMessageToFile(string msg)
{
    System.IO.StreamWriter sw = System.IO.File.AppendText(
        GetTempPath() + "My Log File.txt");
    try
    {
        string logLine = System.String.Format(
            "{0:G}: {1}.", System.DateTime.Now, msg);
        sw.WriteLine(logLine);
    }
    finally
    {
        sw.Close();
    }
}&lt;/PRE&gt;
&lt;P&gt;With this simple method, all you need to do is to pass in a string like this:&lt;/P&gt;&lt;PRE&gt;LogMessageToFile("Hello, World");&lt;/PRE&gt;
&lt;P&gt;The current date and time are automatically inserted to the log file along with your message.&lt;/P&gt;
&lt;P&gt;[author: &lt;A href="http://www.saunalahti.fi/janij/"&gt;Jani Järvinen&lt;/A&gt;, C# MVP]&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=562555" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Debugger_2F00_Debugging/default.aspx">Debugger/Debugging</category></item></channel></rss>