how to structure a window to monitor website

J

JRL

Hi,

I'm interested in some direction and sample code for a function. I am open
to links if this exists already.

I am hosting an IIS website on my vista pc, using visual studio 2008 and
coding in C#.

I would like assistance with structuring this function: when there are
changes to session variables and application variables, I would like to post
the change (a simple text string) to another webpage on the site. Secondly I
would like it to create a new window on the host pc, and post the same
information (like a log file).

I already have code that creates and updates these session and application
variables, so all I need would be examples of how to write this to another
web page in the same site, and to write this to a pop up application monitor
window. Sorry if this seems very basic, but I just need a very simple
example of the recommended structure for this code.

Can you provide a simple template for these two functions?
 
J

JRL

Just wanted to clarify
when I said "If I am not on site," I meant, if I am not physically at the
hosting computer, I can view the website through an administrator only
page,"
 
C

Colbert Zhou [MSFT]

Hello Jeff,

Based on my understanding, you want to log the session variables' changing.
And the logs will display in another web page, as well as a Window in the
same server. Am I right? If I understand incorrectly, please feel free to
let me know.

Typically, ASP.NET site usually keeps logs in a text file, but will not
show them synchronously into other pages or windows forms. And of course
there are not such existed functions as far as I know. If you really want
to achieve that, the simplest way is creating a page or form, and using a
Timer to regularly retrieve the log file and display its content.

Here is a very good article about how to log into text file using ASP.NET
and C#,
http://www.codeproject.com/KB/trace/createlogfiles.aspx

And documents for Windows Form Timer,
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

In the Timer's Tick event handle, we can read the written log file one line
a time, and then display in the Windows Form ListView control. Regarding to
how to read a text file one line at time, please read,
http://msdn.microsoft.com/en-us/library/94223t4d.aspx

Please let me know if these help. If you have any future questions or
concerns, please update the thread and I will do follow up! Good day Jeff!


Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
J

JRL

Colbert,

I see the approach you are recommending, and I have begun to build this as
you propose. I have the error file writing ok. And now I am bringing in the
timer function.

One little problem I have when compiling the timer code you gave, is that I
think I need a reference to a namespace (for using the messagebox function),
but I don't know which one to add. I think it should include 'using
System.Windows.Forms;' but the compiler gives the error:

Error 8 The type or namespace name 'Windows' does not exist in the namespace
'System' (are you missing an assembly reference?)
F:\Public\webdev\Staging\logfiles\App_Code\createTimer.cs 4 14
F:\...\logfiles\

When I hear back, I can carry on.
 
C

Colbert Zhou [MSFT]

Hello Jeff,

The Web Application by default does not reference the Windows Form
assembly. If we need to use Classes in it, we can reference the Windows
Form assembly manually. The followings are steps to do so,

1. In your Solution Window, right click the References folder
2. Click Add Reference button
3. Then, the Add Reference dialog appears
4. In the .NET tab, select the System.Windows.Form, and click OK button

After referencing the System.Windows.Form assembly, we can import the
System.Windows.Form namespace as you tried to do and go on the working.


Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove ¡®online.¡¯)
Microsoft Online Community Support
 
J

JRL

Thank you. Reference is now in. Next little question is how to call the timer?
Currently there is a timer class. Now I need to activate it on the web page.
I tried calling it from the main web page:

protected void Page_Load(object sender, System.EventArgs e)
{
createTimer mytimer = new createTimer();
}

Now, how would I call the timer, so I can see it running. I see the TimerEventProcessor() in the code, but I'm not sure how to call it so it shows on the page.
 
C

Colbert Zhou [MSFT]

Hello Jeff,

The main web page logs the session changing. It is the log view page use
the timer to regularly retrieve the logs and display them. So we use the
Timer in log view page or window, instead of the main page. I have written
a sample for this and sent it to your email box, please have a check.

How to implement the function depends on whether the log view host is a Web
page or Windows Form,

1.If it is a Web page, we just drag the ScriptManager and Timer control
onto that page. Then the page will be loaded regularly according to the
Interval property set by us. In my sample, you will see the following codes
in LogViewer.aspx.cs file,
protected void Page_Load(object sender, EventArgs e)
{
this.ListBox1.Items.Clear();
DisplayLog();
}

private void DisplayLog()
{
using (FileStream fs = File.OpenRead("log.txt"))
{
using (StreamReader sr = new StreamReader(fs))
{
while (!sr.EndOfStream)
{
this.ListBox1.Items.Add(sr.ReadLine());
}
}
}
}

2.If it is a Windows Form we want to show our log, we need to create a new
thread to show the Windows Form in order not to hang the Web UI. After
showing the Windows Form, we create a Windows.Forms.Timer and listen to its
Ticked event. In the event handle function, we update the log. Codes are
like, and you can also find them in my sample's Default.aspx.cs file.

System.Windows.Forms.Form logView = new System.Windows.Forms.Form();
System.Windows.Forms.ListBox lb = new
System.Windows.Forms.ListBox();

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Threading.Thread t = new
System.Threading.Thread(CreateLogViewWindow);
t.Start();
}
}

void CreateLogViewWindow()
{
logView.Load += new EventHandler(logView_Load);
lb.Dock = System.Windows.Forms.DockStyle.Fill;
logView.Controls.Add(lb);
System.Windows.Forms.Application.Run(logView);
}

void logView_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Enabled = true;
t.Interval = 5000;
t.Tick += new EventHandler(t_Tick);
}

void t_Tick(object sender, EventArgs e)
{
lb.Items.Clear();
DisplayLog();
}

private void DisplayLog()
{
using (FileStream fs = File.OpenRead("log.txt"))
{
using (StreamReader sr = new StreamReader(fs))
{
while (!sr.EndOfStream)
{
lb.Items.Add(sr.ReadLine());
}
}
}
}

Please let me know if this addresses your requirement. Thanks and have a
good day, Jeff!


Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
J

JRL

Colbert,

this addresses my requirement most perfectly!

Thank you so much for your help.

The sample works well, and I think I will have success in integrating this
into my work.

JRL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top