I have implemented Application_error method in Global.asax file to log and
send email for all unhandled exceptions. This works perfectly fine when
running on development server, but as soon as I publish it to the production
server, the Application_event stops firing. No log entry is made to the text
file and no email is sent.
PrecompiledApp.config file is present in the root directory, along with
App_global.asax.compiled, App_global.asax.dll in the bin folder. Global.asax
file is not present after I publish the website.
I have tried removing PrecompiledApp.config file, but it doesn't work.
I have tried adding Global.asax file to the root directory, but doesn't work.
I have tried using aspnet_compiler tool to compile the website, but doesn't
work.
I have tried with
It returns ApplicationInstance: ASP.global_asax, which should be returned
according to a post. Then why is the event not firing?
I added a line of code in Session_start event of Global.asax file and it worked. This means that Global.asax is getting deployed successfully. But why is Application_error event not getting executed in case of an error? Any ideas?
Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error Application.Add("ErrorDescription", Server.GetLastError.ToString) End Sub
I don't want to capture the errors in Page_Error event because I'll have to replicate the same code in every page. That's the reason I'm using Application_error.
The links that you provided mostly talk about creating these events, but I already have this event in place. It looks like this:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occur
Exception ex = HttpContext.Current.Server.GetLastError().GetBaseException();
if (ex is ThreadAbortException)
return;
//write error log to a text file
string logFile = Server.MapPath("xxx.txt");
StreamWriter sw = new StreamWriter(logFile, true);
sw.WriteLine("-----------------------------------------------------------------------------------------");
sw.WriteLine("User ID: " + Request.QueryString["xxx"]);
sw.WriteLine("Error message: "+ex.Message);
sw.WriteLine("Error occurred on: " + DateTime.Now.ToString());
sw.WriteLine("Error Stack Trace: " + ex.StackTrace);
sw.WriteLine();
sw.Flush();
sw.Close();
//send email to support team about the error
MailMessage mail = new MailMessage("xxx@xxx.com", "yyy@yyy.com");
mail.Subject = "Error";
mail.Priority = MailPriority.High;
mail.IsBodyHtml = false;
mail.Body = "An error occurred for this user: " + Request.QueryString["xxx"] + "\nMessage: " + ex.Message + "\n\nPlease consult the error log file for more details.";
SmtpClient smtp = new SmtpClient("xxx",00);
smtp.Send(mail);
My guess is that the account the appplication is running under does not have rights to create your xxx.log file or there is some other problem with that first bit of code and so that is causing an error, and therefore because you have no error handling in
your Application_Error method it just falls out. You could try just putting a redirect in the top of the method to some weird page and if it goes to your weird page you know it is definitely hitting the code then you just have to resolve why it is failing.
Thank you frez for pointing me to the right direction.
Commenting out the code except a redirect to a page, did the trick. It redirected successfully, so now I know that Application_error is being executed and there is something wrong with the part of code where I am writing to a text file.
I'll put some error handling in the code and fix the issue.
Checkout NLog (http://nlog-project.org/). You can then just have one logging statement in your Application_Error and use NLogs config file to specify where the log information should go (i.e. file, email or both). You
can then change the config file at any time without having to redploy.
Another option would be to use ELMAH (http://code.google.com/p/elmah/). Then you don't need anything in your code at all, it just needs changes to your web.config.
I am facing a similar issue as the original post, but in my case, I for some reason can't get breakpoints in the Application_Start and Application_End events to hit. both events are executed (as I discovered using your pointer above), but they fail to write
to the error log The problem appears on my development machine. I have been using Cassini until recently but then switched to IIS (hosted on my development machine). Does this have to do with the fact that I cannot get the breakpoints to hit?
Also, if it is indeed a permissions issue, should I be checking the account IIS is running under? How can I see that?
KhD
Member
97 Points
42 Posts
Application_error event in Global.asax not firing after publishing
Jul 22, 2011 04:25 AM|LINK
Hi,
I have implemented Application_error method in Global.asax file to log and
send email for all unhandled exceptions. This works perfectly fine when
running on development server, but as soon as I publish it to the production
server, the Application_event stops firing. No log entry is made to the text
file and no email is sent.
PrecompiledApp.config file is present in the root directory, along with
App_global.asax.compiled, App_global.asax.dll in the bin folder. Global.asax
file is not present after I publish the website.
I have tried removing PrecompiledApp.config file, but it doesn't work.
I have tried adding Global.asax file to the root directory, but doesn't work.
I have tried using aspnet_compiler tool to compile the website, but doesn't
work.
I have tried with
<customErrors mode="Off" />
and
<customErrors mode="On" defaultRedirect="Error Page.aspx" />
in web.config file....nothing works.
I added a line to Page_load of a page:
Response.Write("<br/>ApplicationInstance: " + Context.
ApplicationInstance.GetType().FullName);
It returns ApplicationInstance: ASP.global_asax, which should be returned
according to a post. Then why is the event not firing?
I added a line of code in Session_start event of Global.asax file and it worked. This means that Global.asax is getting deployed successfully. But why is Application_error event not getting executed in case of an error? Any ideas?
Please Help!!!
Thanks!!!
princeG
Star
9612 Points
1602 Posts
Re: Application_error event in Global.asax not firing after publishing
Jul 22, 2011 07:27 AM|LINK
Please check below link
http://forums.asp.net/p/1341132/2716489.aspx
http://support.microsoft.com/kb/306355
alternation. use Page_Error to handled Error
http://weblogs.asp.net/scottgu/archive/2006/08/12/Tip_2F00_Trick_3A00_-Show-Detailed-Error-Messages-to-Developers.aspx
global.asax asp.net
KhD
Member
97 Points
42 Posts
Re: Application_error event in Global.asax not firing after publishing
Jul 22, 2011 07:55 AM|LINK
Hi PrinceG,
Thanks for your reply.
I don't want to capture the errors in Page_Error event because I'll have to replicate the same code in every page. That's the reason I'm using Application_error.
The links that you provided mostly talk about creating these events, but I already have this event in place. It looks like this:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occur
Exception ex = HttpContext.Current.Server.GetLastError().GetBaseException();
if (ex is ThreadAbortException)
return;
//write error log to a text file
string logFile = Server.MapPath("xxx.txt");
StreamWriter sw = new StreamWriter(logFile, true);
sw.WriteLine("-----------------------------------------------------------------------------------------");
sw.WriteLine("User ID: " + Request.QueryString["xxx"]);
sw.WriteLine("Error message: "+ex.Message);
sw.WriteLine("Error occurred on: " + DateTime.Now.ToString());
sw.WriteLine("Error Stack Trace: " + ex.StackTrace);
sw.WriteLine();
sw.Flush();
sw.Close();
//send email to support team about the error
MailMessage mail = new MailMessage("xxx@xxx.com", "yyy@yyy.com");
mail.Subject = "Error";
mail.Priority = MailPriority.High;
mail.IsBodyHtml = false;
mail.Body = "An error occurred for this user: " + Request.QueryString["xxx"] + "\nMessage: " + ex.Message + "\n\nPlease consult the error log file for more details.";
SmtpClient smtp = new SmtpClient("xxx",00);
smtp.Send(mail);
//Server.Transfer("Error Page.aspx");
//Server.ClearError();
HttpContext.Current.ClearError();
Response.Redirect("Error Page.aspx");
}
But this event is not firing on production server. Please let me know if you have any other idea. Thanks !!!
global.asax asp.net
frez
Contributor
5418 Points
913 Posts
Re: Application_error event in Global.asax not firing after publishing
Jul 22, 2011 08:16 AM|LINK
My guess is that the account the appplication is running under does not have rights to create your xxx.log file or there is some other problem with that first bit of code and so that is causing an error, and therefore because you have no error handling in your Application_Error method it just falls out. You could try just putting a redirect in the top of the method to some weird page and if it goes to your weird page you know it is definitely hitting the code then you just have to resolve why it is failing.
global.asax asp.net
KhD
Member
97 Points
42 Posts
Re: Application_error event in Global.asax not firing after publishing
Jul 22, 2011 08:52 AM|LINK
Thank you frez for pointing me to the right direction.
Commenting out the code except a redirect to a page, did the trick. It redirected successfully, so now I know that Application_error is being executed and there is something wrong with the part of code where I am writing to a text file.
I'll put some error handling in the code and fix the issue.
Thank you for your help !!!
global.asax asp.net
frez
Contributor
5418 Points
913 Posts
Re: Application_error event in Global.asax not firing after publishing
Jul 22, 2011 09:10 AM|LINK
Cool.
Checkout NLog (http://nlog-project.org/). You can then just have one logging statement in your Application_Error and use NLogs config file to specify where the log information should go (i.e. file, email or both). You can then change the config file at any time without having to redploy.
Another option would be to use ELMAH (http://code.google.com/p/elmah/). Then you don't need anything in your code at all, it just needs changes to your web.config.
KhD
Member
97 Points
42 Posts
Re: Application_error event in Global.asax not firing after publishing
Jul 22, 2011 09:54 AM|LINK
Thank you. I'll check out these products.
cloucas
Member
64 Points
65 Posts
Re: Application_error event in Global.asax not firing after publishing
Mar 01, 2012 05:39 AM|LINK
Hi
I am facing a similar issue as the original post, but in my case, I for some reason can't get breakpoints in the Application_Start and Application_End events to hit. both events are executed (as I discovered using your pointer above), but they fail to write to the error log The problem appears on my development machine. I have been using Cassini until recently but then switched to IIS (hosted on my development machine). Does this have to do with the fact that I cannot get the breakpoints to hit?
Also, if it is indeed a permissions issue, should I be checking the account IIS is running under? How can I see that?
thanks
chris
global.asax asp.net