Error Handling: Catching Errors With ASP.Net and C# (Part 3)
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Error Handling: Catching Errors With ASP.Net and C# (Part 3)
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Comparison Between Properties and Indexers [next Lesson]  Making Socket Base Application Using TCPListner and TCPClient
Error Handling: Catching Errors With ASP.Net and C# (Part 3)
  by: wujen

Error Handling: Catching Errors With ASP.Net and C# (Part 3)

by: Wujen

Error Logging to the Windows Error Log

The namespace that you would use for this is System.Diagnostics and that would be added on the top of the page:
<%@ Import Namespace="System.Diagnostics"%>
The class EventLog will allow you to read and write to the event log; as well, as create a new one.

First of all you would need to create a new log that you could write in it. It being a specify event source.

The CreateEventSource() method can be use to create both a source and a log.

So here is a basic sample how to do it: (entry2log.aspx)
<%@ Import Namespace="System.Diagnostics"%>

<script language="c#" runat="server" >

   void Entry2Log()

   {

     int [] array = new int[12]

     for( int intCounter=0; intCounter <= 12;intCounter++)

     {

       array[intCounter] = intCounter;

       Response.Write("The value of this counter is:" 
                + intCounter + "<br />");

      }

   }

 void Page_Error(object sender,  EventsArgs e)

  {

    string errorMessage = "Error occurred" + Server.GetLastError();

    Server.ClearError();

    string LogName = "MyAppLog";

    string SourceName = "MyAppSource";

    if (!(EventLog.SourceExists(SourceName));

    {

        EventLog.CreateEventSource(SourceName, LogName);

     }

   EventLog MyLog = new EventLog();

   MyLog.Source = SourceName;

MyLog.WriteEntry(errorMessage, EventLogEntryType.Error);

}

</script>

<%

Entry2Log

%>

This code is not that interesting on the front end at all. The points of interest are with in your EventViewer; where your error logging has taken place. To open your event viewer, click start button, then select Setting; click control panel; double click administrative tools; then double click event viewer.

From there, you will see MyAppLog under the Name column. If you click on this; you will see the custom error that the aspx page created. Now if you click on the error it self; then you would see the full details of this error.

Alternatively, we could do all this in the Application_Error() part of the global.asax page. Doing this would make a master log of all the errors throughout the application.

Error Logging to the Webmaster/Administrator via Email

This part would use System.Web.Mail instead of System.Diagnostics.

So by using the code above; we will just alter a little bit of it and rename it to SendEmailTest.aspx:
<%@ Import Namespace = "System.Web.Mail" %>

<script language="c#" runat="server">

 void sendMailTest()

    {

     int [] array = new int[12]

     for( int intCounter=0; intCounter <= 12;intCounter++)

     {

       array[intCounter] = intCounter;

       Response.Write("The value of this counter is:" 
                            + intCounter + "<br />");

      }

   }

 void Page_Error(object sender, EventArgs e)

 string errorMessage = "Error occurred" + Server.GetLastError();

 Server.ClearError();

 MailMessage newMail = new MailMessage();

 newMail.From = "x@y.com"; // whatever your address needs are

 newMail.To = "z@y.com"; // whatever your address needs are

 newMail.Subject = "This error has occurred"

 newMail.Body =  errorMessage;

 SmtpMail.Send(newMail);

}

</script>

<%

 sendMailTest();

%>

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Error Handling: Catching Errors With ASP.Net and C# (Part 3)
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Comparison Between Properties and Indexers [next Lesson]  Making Socket Base Application Using TCPListner and TCPClient


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
sksandz 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center