ASP.Net/Development/Trace

Материал из .Net Framework эксперт
Версия от 11:51, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Is trace enabled (VB.net)

<%@ Page Language="vb" %>
<html>
   <head>
      <title></title>
      <script runat="server">
         Sub Page_Load()
            If Trace.IsEnabled = True Then
               Message.Text = "Tracing is enabled."
            Else
               Message.Text = "Tracing is not enabled."
            End If
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>



Page Trace (C#)

<%@ Page Language="C#" Debug="true" Trace="true" %>
<script runat="server">
    void Page_Init()
    {
        Trace.Write("NOTE - First line of Page_Init");
    }
</script>
<html>
<head>
    <title>Demonstration of Page Events</title>
</head>
<body>
    <form runat="server">
        <asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
        <br />
    </form>
</body>
</html>



Page Trace in code behind (C#)

<%@ Page language="c#" src="SimpleTrace.aspx.cs" AutoEventWireup="false" Inherits="SimpleTrace" %>
<HTML>
  <body>
    <FORM id="Form1" method="post" runat="server">
      <asp:Label id="Label1" runat="server">A Simple Tracing Example</asp:Label><BR>
      <BR>
      <asp:Button id="cmdTrace" runat="server" Width="104px" Text="Trace"></asp:Button><BR>
      <BR>
    </FORM>
  </body>
</HTML>
<%--
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
  public class SimpleTrace : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Button cmdTrace;
  
    private void Page_Load(object sender, System.EventArgs e)
    {
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      InitializeComponent();
      base.OnInit(e);
    }
    
    private void InitializeComponent()
    {    
      this.cmdTrace.Click += new System.EventHandler(this.cmdTrace_Click);
      this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    private void cmdTrace_Click(object sender, System.EventArgs e)
    {
      Trace.IsEnabled=true;
    }
  }

--%>



Page trace mode (VB.net)

<%@ Page trace= "true" %>
<script runat = "server">
Public Function Subtract( intFirst As Integer, intSecond As Integer) As Integer
  Trace.Write( " intFirst : ", intFirst)
  Trace.Write("intSecond : ", intSecond)
  return intFirst - intSecond
End Function
</script>
The value of 45 minus 30 is : <%=Subtract(45, 30) %>



Trace asp calendar event (C#)

<%@ Page Language="C#" Trace="true" %>
<script runat="server">
    void Page_Load()
    {
        Label1.Text = "www.nfex.ru";
        Calendar1.TodaysDate = DateTime.Now;
    }
    
</script>
<html>
<head>
    <title>Show Trace</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="Label1"
        Runat="server" />
    <asp:Calendar
        id="Calendar1"
        TodayDayStyle-BackColor="Yellow"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>



Trace for loop (VB.net)

<%@ Page Language="VB" Trace="true" %>
<script runat="server">
    Sub Page_Load()
        For counter As Integer = 0 To 9
            ListBox1.Items.Add("item " & counter.ToString())
            Trace.Warn("counter=" & counter.ToString())
        Next
    End Sub
    
</script>
<html>
<head>
    <title>Page Trace</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:ListBox
        id="ListBox1"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>



Use Trace to debug (VB.net)

<%@ Page Language="VB" Debug="true" Trace="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Sqlclient" %>
<script runat="server">
    Sub Button1_Click(sender As Object, e As EventArgs)
      Trace.Write ("Click", "Start")
      Dim conn As New _
           SqlConnection("Server=foo;Database=pubs;Trusted_Connection=true") 
      Dim cmd As New SqlCommand("select * from authors", conn)
      Try
        Trace.Write("Opening connection")
        Conn.Open()
        DataGrid1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        DataGrid1.DataBind()
      Catch ex As Exception
        Trace.Warn(ex.Message)
        Label1.Text = "Could not connect to the database - " & _
                      "please try again later."
      End Try
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
        <asp:Label id="Label1" runat="server">Label</asp:Label>
        <!-- Insert content here -->
    </form>
</body>
</html>



Write debug information to trace log (C#)

<%@ Page trace= "true" %>
<script Language="c#" runat = "server">
  public int Subtract(int intFirst, int intSecond)
  {
    Trace.Write(" intFirst : ", intFirst.ToString());
    Trace.Write("intSecond : ", intSecond.ToString());
    return intFirst - intSecond;
  }
</script>
The value of 45 minus 30 is : <%=Subtract(45, 30) %>



Write to Trace log in event action (C#)

<%@ Page language="c#" src="TraceExample.aspx.cs" AutoEventWireup="false" Inherits="TraceExample" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <DIV style="FONT-SIZE: x-small; WIDTH: 416px; FONT-FAMILY: Verdana; POSITION: relative; HEIGHT: 152px" ms_positioning="GridLayout">
        <asp:Button id="cmdWrite" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 64px" runat="server" Text="Write Message" Width="128px" Height="24px" Font-Size="X-Small" Font-Names="Verdana"></asp:Button>
        <asp:Label id="Label1" runat="server" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 16px">A Simple Tracing Example</asp:Label>
        <asp:Button id="cmdWriteCategory" style="Z-INDEX: 103; LEFT: 160px; POSITION: absolute; TOP: 64px" runat="server" Text="Write Message With Category" Width="216px" Height="24px" Font-Size="X-Small" Font-Names="Verdana"></asp:Button>
        <asp:Button id="cmdError" style="Z-INDEX: 104; LEFT: 16px; POSITION: absolute; TOP: 96px" runat="server" Text="Write Exception" Width="128px" Height="24px" Font-Size="X-Small" Font-Names="Verdana"></asp:Button>
        <asp:Button id="cmdSession" style="Z-INDEX: 105; LEFT: 160px; POSITION: absolute; TOP: 96px" runat="server" Text="Add Session Item" Width="136px" Height="24px" Font-Names="Verdana"></asp:Button></DIV>
    </form>
    <BR>
  </body>
</HTML>

<%--
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
  public class TraceExample : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Button cmdWrite;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Button cmdWriteCategory;
    protected System.Web.UI.WebControls.Button cmdError;
    protected System.Web.UI.WebControls.Button cmdSession;
  
    private void Page_Load(object sender, System.EventArgs e)
    {
      Trace.IsEnabled = true;
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.cmdWrite.Click += new System.EventHandler(this.cmdWrite_Click);
      this.cmdWriteCategory.Click += new System.EventHandler(this.cmdWriteCategory_Click);
      this.cmdError.Click += new System.EventHandler(this.cmdError_Click);
      this.cmdSession.Click += new System.EventHandler(this.cmdSession_Click);
      this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    private void cmdWrite_Click(object sender, System.EventArgs e)
    {
      
      Trace.Write("About to place an item in session state.");
      Session["Test"] = "Contents";
      Trace.Write("Placed item in session state.");
    }
    private void cmdWriteCategory_Click(object sender, System.EventArgs e)
    {
      Trace.Write("Page_Load", "About to place an item in session state.");
      Session["Test"] = "string in session";
      Trace.Write("Page_Load", "Placed item in session state.");
    }

    private decimal DivideNumbers(decimal number, decimal divisor)
    {
      return number/divisor;
    }
    private void cmdError_Click(object sender, System.EventArgs e)
    {
      try
      {
        DivideNumbers(5, 0);
      }
      catch (Exception err)
      {
        Trace.Warn("cmdError_Click", "Caught Error", err);
      }
    }
    private void cmdSession_Click(object sender, System.EventArgs e)
    {
      DataSet ds = new DataSet();
      Session["MyDataSet"] = ds;
    }
  }

--%>



Write to Trace (VB.net)

<%@ Page Trace="true"%>
<script language="VB" runat="server">
 Sub WriteToTrace()
     Dim intCounter as integer
     intCounter=1
     Trace.Write ("FirstCategory", "Variable is initialized")
     do while intCounter > 10
      intCounter = intCounter + 1
     loop 
     if intCounter < 10 then 
         Trace.Warn("ErrorCategory", "Value of intCounter is not incrementing")
     end if
 end Sub
</script>
<%
 WriteToTrace()
%>



Write trace warn (VB.net)

<%@ Page Language="VB" Trace="True" %>
<html>
<head>
   <title>Tracing Sample</title>
   <script runat="server">
      Sub Page_Load()
         If Page.Trace.IsEnabled = True Then
            Trace.Write("MyCategory", "Hello, Trace!")
            Trace.Warn("MyCategory", "This text will be red!")
            Message.Text = "Tracing is enabled for this page."
         Else
            Message.Text = "Tracing is not enabled for this page."
         End If
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>