Is there a way to allow developer machine to see asp.net errors?
Our site doesn't show the exception to the user. But having to be in the local server to see the error is something really bad for us.
The question is:
How to al开发者_如何学Pythonlow developer machines to see the errors in production?
EDIT: Could I do this by altering Web.config file? You guys showed up with some interesting ideas, but I cannot alter right now the application.
Check out Elmah, http://code.google.com/p/elmah/
I think the stack overflow guys use it too.
Try putting this in your Global.asax
void Application_Error(Object sender, EventArgs E)
{
// Code that runs when an unhandled error occurs
string strError = "Error in: " + Request.Path +
"\nUrl: " + Request.RawUrl + "\n\n";
// Get the exception object for the last error message that occured.
Exception ErrorInfo = Server.GetLastError().GetBaseException();
strError += "Error Message: " + ErrorInfo.Message +
"\nError Source: " + ErrorInfo.Source +
"\nError Target Site: " + ErrorInfo.TargetSite +
"\n\nQueryString Data:\n-----------------\n";
// Gathering QueryString information
for (int i = 0; i < Context.Request.QueryString.Count; i++)
strError += Context.Request.QueryString.Keys[i] + ":\t\t" + Context.Request.QueryString[i] + "\n";
strError += "\nPost Data:\n----------\n";
// Gathering Post Data information
for (int i = 0; i < Context.Request.Form.Count; i++)
strError += Context.Request.Form.Keys[i] + ":\t\t" + Context.Request.Form[i] + "\n";
strError += "\n";
if (User.Identity.IsAuthenticated) strError += "User:\t\t" + User.Identity.Name + "\n\n";
strError += "Exception Stack Trace:\n----------------------\n" + Server.GetLastError().StackTrace +
"\n\nServer Variables:\n-----------------\n";
// Gathering Server Variables information
for (int i = 0; i < Context.Request.ServerVariables.Count; i++)
strError += Context.Request.ServerVariables.Keys[i] + ":\t\t" + Context.Request.ServerVariables[i] + "\n";
strError += "\n";
// Sending error message to administration via e-mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(new System.Net.Mail.MailAddress("youremailadrress"));
mail.From = new System.Net.Mail.MailAddress("youremailadrress");
mail.Subject = "SDI Error";
mail.Body = strError;
System.Net.Mime.ContentType("text/plain"));
System.Net.Mime.ContentType("text/html"));
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("YourSMTPServer");
smtp.Send(mail);
}
If the exception does not contain sensitive information, you may consider writing it out to your "friendly" error page as a comment. This way it does not show up on the page, but a dev can view source and see the info.
There isn't a way to show errors for some machines (other than local) and not for everyone else. You should use the global application error event in global.asax to record the exception.
protected void Application_Error(object sender, EventArgs e)
{
System.Exception ex = Server.GetLastError();
//TODO: Log Exception
}
We use the global exception error event to both log the error into a Db table and to email a comprehensive list of error state to the complete dev team. The information in the email includes:
Request information CallStack User/Client Information Complete dump of Session Complete dump of cookies Complete dump of Application variables
Sending this as an email to all devs also has the added benefit of keeping all problems visible, and being immediately obvious if there is a major problem - your email box starts filling up.
It depends on how you're telling the web server to show custom error pages. If you're doing it through web.config (the system.web customErrors element), then you could just have your development box with a different web.config than that of production.
You could use the remote debug tool for visual studio 2008. Look here for more info: http://msdn.microsoft.com/en-us/library/bt727f1t.aspx
You could turn on tracing in the web.config, then open http://your-server/trace.axd to see reports about requests to the server, including exceptions thrown.
See http://msdn.microsoft.com/en-us/library/1y89ed7z(VS.71).aspx for more information.
In a nutshell, you'll add the following to your web.config.
<configuration>
<system.web>
<trace enabled="true" requestLimit="100" localOnly="false"/>
</system.web>
</configuration>
Since you want a solution that is part of the framework, and is something that you can enable and disable in the web.config, I believe this is your best solution.
精彩评论