Last post Mar 01, 2012 06:39 AM by cloucas
Jul 22, 2011 05:25 AM|KhD|LINK
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
I have tried with
<customErrors mode="Off" />
<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.
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?
Jul 22, 2011 08:27 AM|princeG|LINK
Please check below link
alternation. use Page_Error to handled Error
Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error Application.Add("ErrorDescription", Server.GetLastError.ToString) End Sub
Jul 22, 2011 08:55 AM|KhD|LINK
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)
//write error log to a text file
string logFile = Server.MapPath("xxx.txt");
StreamWriter sw = new StreamWriter(logFile, true);
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);
//send email to support team about the error
MailMessage mail = new MailMessage("email@example.com", "firstname.lastname@example.org");
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);
But this event is not firing on production server. Please let me know if you have any other idea. Thanks !!!
Jul 22, 2011 09:16 AM|frez|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.
Jul 22, 2011 09:52 AM|KhD|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 !!!
Jul 22, 2011 10:10 AM|frez|LINK
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.
Jul 22, 2011 10:54 AM|KhD|LINK
Thank you. I'll check out these products.
Mar 01, 2012 06:39 AM|cloucas|LINK
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?