Write to Trace log in event action (C#) : Trace « Development « ASP.Net

Write to Trace log in event action (C#)

<%@ Page language="c#" src="TraceExample.aspx.cs" AutoEventWireup="false" Inherits="TraceExample" %>
    <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>

using System;
using System.Collections;
using System.ComponentModel;
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.
    /// <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);


    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)
        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;



Related examples in the same category

1.Is trace enabled (VB.net)
2.Trace for loop (VB.net)
3.Write to Trace (VB.net)
4.Use Trace to debug (VB.net)
5.Page trace mode (VB.net)
6.Write trace warn (VB.net)
7.Trace asp calendar event (C#)
8.Page Trace in code behind (C#)
9.Write debug information to trace log (C#)
10.Page Trace (C#)