subjectivist subjectivist - 4 months ago 9
ASP.NET (C#) Question

How do I write debug info in a C# page

Note: This problem was occurring because the render event was being overridden for the page, which in turn caused the .net error display that would have been visible to instead display garbage, and the coding error in the class was not seen.




I want to place debug info a C# web page. I made a static class containing an array and debug messages are added at various places in code. The class has a get method returning a string having all array elements concatenated. At the bottom of the page I have :

</body>
<!--debug=<%=debugging.get()%> -->
</html>

This always screws up the page. The entire page displays garbage. If I use a text constant such as the following it works OK:

</body>
<!--debug=<%="xxx"%> -->
</html>

I'm thinking it may be a unicode issue or something. I've tried conversions and can't get it to work. The class is:

using System;

public static class debugging {
public static String[] messages;

public static void add(String v) {
Array.Resize(ref messages, messages.Length + 1);
messages[messages.Length - 1] = v;
}

public static String get() {
String s = "";
for (int i = 0; i < messages.Length; i++) s += messages[i] + "\r\n";
return s;
}
}


How can I get this to work?

Answer

You class is buggy, and probably that's why you get the garbage as you say. Your message parameter throw exception as it is. Here is a working class.

public static class debugging
{
    public static String[] messages = null;

    public static void add(String v)
    {
        if (messages == null)
            messages = new string[1];
        else
            Array.Resize(ref messages, messages.Length + 1);

        messages[messages.Length - 1] = v;
    }

    public static String get()
    {
        if (messages == null) 
           return string.Empty;
        String s = "";
        for (int i = 0; i < messages.Length; i++) s += messages[i] + "\r\n";
        return s;
    }
}

and here is an improvement version of that class

public static class debugging
{
    public static List<string> cMsg = new List<string>();

    public static void add(String v)
    {
        cMsg.Add(v);
    }

    public static String get()
    {
        StringBuilder sbOnMe = new StringBuilder();

        foreach (var One in cMsg)
            sbOnMe.AppendLine(One);

        return sbOnMe.ToString();
    }
}
Comments