How to debug and/or trace execution flow in WebMatrix?
Asked Answered
S

3

8

I am just getting into web development (from a Windows application development background), and WebMatrix seems like a good place to start, due to it's simplicity, and also because it looks like a useful stepping stone towards full ASP.NET MVC development.

However the lack of debugging tools hurts a bit, especially while trying to learn the fundamentals of development in the web environment.

Tracing the flow of execution, and displaying the trace data on the page, seems like a fairly basic capability for an absolute minimum debugging experience, but even that does not seem to be built into WebMatrix (or maybe I just haven't found it yet).

It is easy within a single page to set a trace variable, and then show that variable in the page layout. But how does that help when I need to trace execution across other pages in the flow (e.g. Layout pages, _PageStart pages, etc), and even within my C# classes used during the page building process.

Is there a tracing capability within WebMatrix that I have not yet found? Or alternatively, is there a way to implement a tracing facility that will work throughout the application, and not just in a single page? Even a third party product ($) would be better than nothing.

Swansea answered 25/2, 2011 at 2:8 Comment(0)
S
5

Part of WebMatrix's simplicity (and for some, it's appeal) is the lack of bloat such as debuggers and tracing tools! Having said that, I wouldn't bet against a debugger appearing in a future release (along with Intellisense).

Within WebMatrix we have basic 'print variables to the page' cababilities with the ServerInfo and ObjectInfo objects which help with dumping raw info to the frontend. A quick tutorial for using these objects can be found on the asp.net site: Introduction to Debugging.

If you want to go deeper with actual IDE level debugging and tracing, then I suggest you use Visual Studio (any version will work fine, including the free Express edition).

Again there is an excellent introduction to doing this on the asp.net site: Program ASP.NET Web Pages in Visual Studio.

The key points are installing Visual Web Developer 2010 Express and ASP.NET MVC3 RTM. This will give you a handy 'Launch Visual Studio' button in WebMatrix too. Don't worry because you are still making Razor Web Pages sites, it just happens to be in Visual Studio.

Snodgrass answered 25/2, 2011 at 4:59 Comment(0)
L
4

There is a Razor Debugger (currently at version 0.1) in the Packages (Nuget) area within WebMatrix.

Lorenzen answered 25/2, 2011 at 5:59 Comment(1)
Seems a bit better of an answer, considering the environment.Wafd
B
1

WebMatrix harkens back to the classic days of debugging via alerts/print. Not ideal, but there's a certain simplicity and art to it. But, it's sometimes tough to get at your variables and whatnot when you have a problem in your code. I've solved most of my debugging issues with a simple Debug class.

Make a file called Debug.cs in your App_Code directory with the following code:

using System;
using System.Collections.Generic;
using System.Web;
using System.Text;

public class TextWrittenEventArgs : EventArgs {
    public string Text { get; private set; }
    public TextWrittenEventArgs(string text) {
        this.Text = text;
    }
}

public class DebugMessages {
  StringBuilder _debugBuffer = new StringBuilder();

  public DebugMessages() {
    Debug.OnWrite += delegate(object sender, TextWrittenEventArgs e) { _debugBuffer.Append(e.Text); };
  }

  public override string ToString() {
    return _debugBuffer.ToString();
  }
}

public static class Debug {
  public delegate void OnWriteEventHandler(object sender, TextWrittenEventArgs e);
  public static event OnWriteEventHandler OnWrite;

  public static void Write(string text) {
    TextWritten(text);
  }

  public static void WriteLine(string text) {
    TextWritten(text + System.Environment.NewLine);
  }

  public static void Write(string text, params object[] args) {
    text = (args != null ? String.Format(text, args) : text);
    TextWritten(text);
  }

  public static void WriteLine(string text, params object[] args) {
    text = (args != null ? String.Format(text, args) : text) + System.Environment.NewLine;
    TextWritten(text);
  }

  private static void TextWritten(string text) {
    if (OnWrite != null) OnWrite(null, new TextWrittenEventArgs(text));
  }
}

This will give you a static class called Debug, which has your typical WriteLine method. Then, in your CSHTML pages, you can new-up the DebugMessages object. You can .ToString() it to get debug messages out.

 var debugMsg = new DebugMessages();
 try {
    // code that's failing, but calls Debug.WriteLine() with key debug info
 }
 catch (Exception ex) {
   <p>@debugMsg.ToString()</p>
 }
Bailment answered 4/1, 2013 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.