Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Error Handling
Message
 
To
20/11/2003 10:30:14
Mindy Shingara
Central Susquehanna Intermediate Unit
Lewisburg, Pennsylvania, United States
General information
Forum:
ASP.NET
Category:
Web forms
Title:
Miscellaneous
Thread ID:
00851881
Message ID:
00852159
Views:
10
I entered code into a method of the global.ascx.cs file. The code below is collected in bits and I doubt it will run as is because I pulled a lot of specialized code that's specific to our app. Hopefully it will give you the idea about how I set this up. It's pretty simple and I'm sure the application block will be more thorough...

HTH,
Chris.

Make sure to include these assemblies (among others):
using System.Web.Mail;
using System.Diagnostics;
protected void Application_Error(Object sender, EventArgs e) {}
This method happens whenever there's an error in the application.

You can collect a bunch of info such as:
// collect info about the current environment
string lcAppName = AppDomain.CurrentDomain.FriendlyName.ToString();
string lcOSVersion = Environment.OSVersion.ToString();
string lcCLRVersion = Environment.Version.ToString();
string lcMachineName = Environment.MachineName;
string lcLocalHostName = System.Net.Dns.GetHostByName("LocalHost").HostName;
string lcURL = Request.RawUrl.ToString() ;
string lcAppPath = Environment.CurrentDirectory.ToString();
Grab some debug/exception info:
// Get exception info
Exception MyException = Server.GetLastError();
String lcErrorString = MyException.ToString();
String lcErrorSource = MyException.Source.ToString();
String lcErrorTarget = MyException.TargetSite.Name.ToString();
String lcErrorISource = MyException.InnerException.Source.ToString();
String lcErrorITarget = MyException.InnerException.TargetSite.Name.ToString();
String lcBaseMsg = MyException.GetBaseException().Message.ToString();
String lcBaseTargetSite = MyException.GetBaseException().TargetSite.ToString();
String lcBaseSource = MyException.GetBaseException().Source.ToString();
String lcMyTrace = lcErrorSource +"::"+ lcErrorTarget;
String lcMyITrace = lcErrorISource +"::"+ lcErrorITarget;
Build the error message:
// build the error message
String lcMessage =  "" ;
lcMessage += System.DateTime.Now.ToString() + " (local)" + " -- " ;
lcMessage += System.DateTime.Now.ToUniversalTime() + " (UTC)" + "\n" + "\n";
lcMessage += "-------------------------------------" + "\n";
lcMessage += "         Base Message: " + lcBaseMsg + "\n" ;
lcMessage += "     Base Target Site: " + lcBaseTargetSite + "\n" ;
lcMessage += "          Base Source: " + lcBaseSource + "\n" ;
lcMessage += "-------------------------------------" + "\n";
lcMessage += "URL that caused error: " + lcURL + "\n" ;
lcMessage += "        Source/Target: " + lcMyTrace + "\n" ;
lcMessage += "  Inner Source/Target: " + lcMyITrace + "\n" ;
lcMessage += "-------------------------------------" + "\n";
lcMessage += "      Portal/Tab Name: " + lcPortalName + " / " + lcTabName + "\n" ;
lcMessage += "     Application Path: " + lcAppPath + "\n" + "\n";
lcMessage += "-------------------------------------" + "\n";
lcMessage += "   NetBIOS Name: " + lcMachineName + "\n" ;
lcMessage += "Local Host Name: " + lcLocalHostName + "\n" ;
lcMessage += " Current Domain: " + lcAppName + "\n" ;
lcMessage += "     OS Version: " + lcOSVersion + "\n" ;
lcMessage += "    CLR Version: " + lcCLRVersion + "\n" + "\n";

lcMessage += "-------------------------------------" + "\n";
lcMessage += "     Server Variables: " + "\n";
lcMessage += lcServerVariables + "\n" + "\n";

lcMessage += "\n" + "-------------------------------------" + "\n";
lcMessage += "Company (web.config): " + lcCompanyName + "\n" ;
lcMessage += "           User Name: " + lcUsername + "\n" ;
lcMessage += "\n" + "-------------------------------------" + "\n";
lcMessage += "Stack Trace: " + "\n" ;
lcMessage += lcErrorString ;
lcMessage += "\n" + "\n";
Create and fill the event log:
// create the event log
if (!(EventLog.SourceExists("MyEventLog"))) 
{
    EventLog.CreateEventSource("MyEventLog", "ThisApp");
}
EventLog aLog = new EventLog();
aLog.Source = "MyEventLog";
aLog.WriteEntry(lcMessage, EventLogEntryType.Error);
Send an email:
// send the email
MailMessage emailMessage = new MailMessage();
emailMessage.Priority = MailPriority.High;
emailMessage.Subject = lcCompanyName + " - Error Alert [" + lcUsername + "]";
emailMessage.To = "you@yada.com";
emailMessage.From = "me@yadayada.com";
emailMessage.Body = lcMessage ;

SmtpMail.SmtpServer = "mail.yadayada.com";
SmtpMail.Send(emailMessage);
>I have my web.config error setting at "Remote Only" so the users don't see any .NET errors they may receive. Now, I need to write the error to an error log, so if users do get errors, I can research and debug. Is the .NET EventLog component the tool I should be using to go about this?

chris jefferies
chris@freeranger.com
Previous
Reply
Map
View

Click here to load this message in the networking platform