Professional Web Applications Themes

calling Trace.Write() from classes besides the page class - ASP.NET General

I've noticed that if you enter the following code in the codebehind page for an .aspx page, it won't compile because the call to Trace.Write() is not valid except in methods of a class derived from System.Web.UI.Page. Two questions: 1) I don't know much about C# but I was under the impression that if certain classes and functions were available in a namespace (as the result of a "using" statement at the top of a file), then they were available throughout the namespace. If Trace.Write() is available as the result of one of the "using" statements at the top of ...

  1. #1

    Default calling Trace.Write() from classes besides the page class

    I've noticed that if you enter the following code in the codebehind
    page for an .aspx page, it won't compile because the call to
    Trace.Write() is not valid except in methods of a class derived from
    System.Web.UI.Page. Two questions:
    1) I don't know much about C# but I was under the impression that if
    certain classes and functions were available in a namespace (as the
    result of a "using" statement at the top of a file), then they were
    available throughout the namespace. If Trace.Write() is available as
    the result of one of the "using" statements at the top of the
    codebehind file, then how is it not available in the MyClass class?
    And if Trace.Write() is not imported by any of the "using" statements,
    then how is it available to the WebForm1 class?
    2) In that case, is there any way to call Trace.Write() statements
    from classes other than the page class, to aid in debugging? I added
    "using System.Diagnostics;" to the beginning of the code so that I
    could call Trace.Write() from the "MyClass" class, but the output
    still didn't show up in the trace.

    -Bennett

    Here's the code:
    *****
    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;

    namespace tracetest
    {
    public class MyClass
    {
    public MyClass()
    {
    Trace.Write("MyCategory", "MyMessage");
    }
    }
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {

    private void Page_Load(object sender, System.EventArgs e)
    {
    Trace.Write("MyCategory", "MyMessage");
    // Put user code to initialize the page here
    }

    #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.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }
    }
    Bennett Guest

  2. #2

    Default Re: calling Trace.Write() from classes besides the page class

    "Steve C. Orr, MCSD" <net> wrote in message news:<phx.gbl>... 

    I tried calling System.Web.TraceContext.Write() in the constructor of
    my class, and then calling the constructor from the Page_Load method,
    but it still didn't write to the trace output. When I run the code
    below as a codebehind for WebForm1, I see the "MyMessage1" and
    "MyMessage2" traces, but I don't see the "MyClassCategory" trace
    called from within the constructor. Am I doing something wrong? I
    assume I'm supposed to initialize the System.Web.TraceContext object
    by passing HttpContext.Current to the constructor?

    Here's the code:

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

    namespace tracetest
    {
    public class MyClass
    {
    public MyClass()
    {
    System.Web.TraceContext x =
    new
    System.Web.TraceContext(HttpContext.Current);
    x.Write("MyClassCategory", "MyMessage");
    }
    }
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {

    private void Page_Load(object sender, System.EventArgs e)
    {
    MyClass x = new MyClass();
    Trace.Write("MyCategory", "MyMessage1");
    Trace.Write("MyCategory", "MyMessage2");
    // Put user code to initialize the page here
    }

    #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.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }
    }
    Bennett Guest

  3. #3

    Default Re: calling Trace.Write() from classes besides the page class

    Hi,

    HttpContext.Current.Trace.Write(); should do the trick.

    Natty Gur, CTO
    Dao2Com Ltd.
    34th Elkalay st. Raanana
    Israel , 43000
    Phone Numbers:
    Office: +972-(0)9-7740261
    Fax: +972-(0)9-7740261
    Mobile: +972-(0)58-888377


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Natty Guest

  4. #4

    Default Re: calling Trace.Write() from classes besides the page class

    Why is it not possible to use the Trace.Write from System.Diagnostics ? The
    code is simply skipped ... It works in an ASP.NET project, but not in
    another ...

    Strange ...

    Any help appreciated.
    Thanks
    Didier Kuttel

    "Bennett Haselton" <org> wrote in message
    news:google.com... 


    Didier Guest

Similar Threads

  1. Replies: 1
    Last Post: October 5th, 11:21 AM
  2. Loading XML using a custom class and accessing it fromother classes?
    By maija_g in forum Macromedia Flash Data Integration
    Replies: 2
    Last Post: August 31st, 10:50 AM
  3. Housing multiple classes under one class
    By Gerard Samuel in forum PHP Development
    Replies: 1
    Last Post: September 21st, 12:31 PM
  4. Replies: 0
    Last Post: July 5th, 02:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139