472,785 Members | 1,049 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,785 software developers and data experts.

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
}
}
Nov 17 '05 #1
5 3001
"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message news:<uM**************@TK2MSFTNGP12.phx.gbl>...
Use System.Web.TraceContext.Write from your classes.

Here's more info:
http://msdn.microsoft.com/library/de...classtopic.asp


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
}
}
Nov 17 '05 #2
"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message news:<uM**************@TK2MSFTNGP12.phx.gbl>...
Use System.Web.TraceContext.Write from your classes.

Here's more info:
http://msdn.microsoft.com/library/de...classtopic.asp


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
}
}
Nov 17 '05 #3
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!
Nov 17 '05 #4
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!
Nov 17 '05 #5
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" <be*****@peacefire.org> wrote in message
news:e6**************************@posting.google.c om...
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
}
}

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: F. GEIGER | last post by:
I've dev'ed a Python prototype of an app, that besides the internals making it up has a gui. While test-driven dev'ing the app's internals in Python is fun as usual, dev'ing the GUI is not so...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
by: Kevin | last post by:
Hi All, Can someone please tell me what the difference is between DEBUG and TRACE. From what I can see, they are two different classes, but with the same members? Whats the purpose of have two...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
0
by: Bennett Haselton | last post by:
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...
5
by: martin | last post by:
Hi, The trace class seems to me to be very usefull however there are a few features of it that have to confused. I am using visual studio 2003. the IDE will not let me write ...
1
by: H Branyan | last post by:
In the Application_OnStart event in global.asax, my code calls an AlertService which creates a System.Timers.Timer() object. So this service doesn't actually run in conjunction with a specific...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: Jerome Cohen | last post by:
AI am trying to call a third-party web service. this service expects an XML fragment that contains the request plus other parameter. adding the web reference created the syntax below(reference.vb)....
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.