web developer tips (15):在ASP.NET Ajax里使用跟踪

原文地址: How to use tracing with ASP.NET Ajax

如果你在使用ASP.NET Ajax的时候,遇到一个页面问题,想在调试时候跟踪语句,有个比较快速的方法:使用“Sys.Debug.trace”函数。

<script language="javascript" type="text/javascript">

function PositionDiv() {
Sys.Debug.trace("Entering PositionDiv Function");
//some other code
Sys.Debug.trace("Exiting PositionDiv Function");
}
</script>

http://www.watch-life.net/visual-studio/use-tracing-with-asp-net-ajax.html

为了看到跟踪输出的内容,在页面上加一个TextArea的控件,控件的ID为“TraceConsole”。这个现实的跟踪的可用性,一旦应用,将意味着将该TextArea必须手动添加到输出。不过,在调试网站的时候,跟踪的内容也被写到Visual Studio的输出窗口,就像FireFox的输出控制台一样。

Sys.Debug 类还有其他许多好的方法,例如:
assert:如果被测试的条件是false的,将会有个消息框来显示消息。
fail:引发程序终止或中断调试。
traceDump:用清晰的格式显示对象数据。

延伸阅读:
1、有关Sys.Debug 参见:http://msdn.microsoft.com/zh-cn/library/bb397422.aspx

2、Debugging and Tracing AJAx Applications

 

下面是在不同的方式来debug AJAX-enabled ASP.NET application



(1)在Configuration文件中授权.

(2)在服务端使用tracing

(3)使用在Sys.Debug类的方法设置breakpoints和handle trace输出

(4)在浏览器材里面授权debugging

(5)追加Visual Studio2008 debugger或在你的浏览器里面扩展工具.

(7)使用扩展工具捕获HTTP的交换.



 


(1)在Configuration文件中授权.






在Configuration中添加一个compilation元素,设置一贯debug属性为"true",如下面:



 

<configuration>

  
<system.web>

    
<compilation debug="true">

      
<!-- etc. -->

    
</compilation>

  
</system.web>

<configuration>





要将应用程序发布,就要从debug该为Release才能发布



在Web.config文件中,如果compilation元素中包括debug,就将里面debug属性该为"false"



确定在ScriptManager中面的ScriptMode属性设置为Release



而在@page中指定的debug属性不会影响ASP.NET AJAX applications.而为ScriptManager在Web.config中设置的IsDebuggingEnabled
and ScriptMode属性是决定是否呈现debug脚本.



 


(2)Tracing on the Server






怎样授权ASP.NET页面Tracing就是这样

<%@ Page Trace="true" %>



而在ASP.NET
AJAX中就必须有一个partial-page呈现授权,也就是要在ScriptManager中将EnablePartialRendering属性设置为"true",

其实就是所说的饿"view debugger trace messages in the Output window". 

 


(3)Debug Helper Class




ASP.NET为调试客户端应用程序提供一个Sys.Debug Class.如果你是IE你可以在你的也面上创建一个





就是使用下面的方法:



Sys.Debug.assert(condition,message,displayCaller)

给出检测条件,并如果条件为false时候,就会有在debugger中提示.



Sys.Debug.clearTrace()

清楚所有的指定在id为"TraceConsole"元素中显示的信息.



Sys.Debug.traceDump(object,name)



抑制一个对象在debugger console和TraceConsole中输出.



Sys.Debug.fail(message)

显示一个message在debugger的输出窗口和TraceConsole的textarea元素中



Sys.Debug.trace(text)

在debugger console和TraceConsole中追加一行文本.

这个是MSDN上的事例

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html >

<head id="Head1" runat="server">

    
<title>Untitled Page</title>

<script language="javascript" type="text/javascript">

function btnAssert_onclick() {

    
var n;

    
// Insert code intended to set n to a positive integer.

    if (false) n = 3;

    
// Assert if n is not greater than 0.

    Sys.Debug.assert(n > 0, "n must be set to a positive integer.");

}




function btnFail_onclick() {

    
var n;

    
// Insert code intended to set n to a numeric value.

    if (false) n = 3;

    
// Fail if n is not numeric.

    if (isNaN(n)) Sys.Debug.fail("The value of n must be a number.");

}




function btnTrace_onclick() {

    v = form1.text1.value;

    Sys.Debug.trace("Name set to " + "\"" + v + "\".");

    alert("Hello " + v + ".");

}




function btnDump_onclick() {

    Sys.Debug.traceDump(form1.text1, "Name textbox");

    alert("Hello " + form1.text1.value + ".");

}




function btnClear_onclick() {

    Sys.Debug.clearTrace()

    alert("Trace console cleared.");

}


</script>

</head>

<body>

<form id="form1" runat="server">

    
<h2>Sys.Debug Methods Test Page</h2>

    
<asp:ScriptManager ID="ScriptManager1" 

        runat
="server" />

    
<p><b>Use these buttons to demonstrate the assert() and fail() 

    methods:
</b><br />

    
<input id="btnAssert" type="button" value="Assert" 

        style
="width: 100px" 

        onclick
="return btnAssert_onclick()" /> &nbsp

    
<input id="btnFail" type="button" value="Fail" 

        style
="width: 100px" onclick="return btnFail_onclick()" />

    
</p><hr />

    
<b>Use the textbox and buttons below to demonstrate tracing.</b>

    
<br />

    
<p>Enter your name here:<br />

    
<input id="text1" maxlength="50" type="text" />

    
<br />

    
<br />

    
<input id="btnTrace" type="button" value="Trace" 

        style
="width: 100px" onclick="return btnTrace_onclick()" /><br />

    
<input id="btnDump" type="button" value="TraceDump" 

        style
="width: 100px" onclick="return btnDump_onclick()" /><br />

    
<input id="btnClear" type="button" value="ClearTrace" 

        style
="width: 100px" onclick="return btnClear_onclick()" /><br />

    
<br /></p>

    View output in the TraceConsole textarea below.

    
<br />

    
<textarea id='TraceConsole' rows="10" cols="50" 

        title
="TraceConsole"></textarea>

</form>

</body>

</html>



 


(4)To enable debugging in Internet Explorer




就是在IE里面设置Disable Script Debugging.

Disable Script Debugging (Other)

Display a notification about every script error

Show friendly HTTP error messages 

都打开就能调试js.



当然还可以使用我们熟悉的那IE的扩展工具.



Attaching the Visual Studio Debugger to Internet Explorer



你要debug客户端的脚本,你必须追加debugger到IE里面.



在Visual Studio debugger中选中Debug menu,选中Attach to Process,只后会有一个Attach to
Process对话框.选种iexplore.exe.



这样就可以拉.如果你使用IE遭遇脚本错误,并且你按这样的设定,但不在当前就追加到debugger中,浏览器会提示你选择一个debugger.你既能继续不debugging或追加一个debugger直接通过代码.



微信扫描下方的二维码阅读本文

那年今日

2008-07-02 c#的细节(一)-问号的细节 (0 个评论)

2008-07-02 WP-DB-Backup 中文版下载 (2 个评论)

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注