Elmah email sending and File.AppendText not working on live server
I recently set up Elmah on the site I'm working on. The actual logging and elmah view page work on both my local and live servers. Elmah email sending is working on my local but not on the live server. I did some looking around and could not find any solutions to this issue but I did find a post where a person suggested adding some code to the Global.asax to have the elmah events log to a file to attempt and debug it. Following that advice I created a global.asax file (the site did not previously have one) and added this code:
protected void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
string tos = (e.Mail.To.Count != 0) ? string.Join(",", e.Mail.To.ToList().Select(x => x.ToString()).ToArray()) : string.Empty;
string msg = string.Format("Sending mail to {0}. ", tos);
string filename = HostingEnvironment.MapPath("~/UploadedFiles/0/ElmahMailLog.txt");
StreamWriter file = File.AppendText(filename);
file.WriteLine(msg);
file.Close();
}
protected void ErrorMail_Mailed(object sender, ErrorMailEventArgs e)
{
string tos = (e.Mail.To.Count != 0) ? string.Join(",", e.Mail.To.ToList().Select(x => x.ToString()).ToArray()) : string.Empty;
string msg = string.Format("Sent mail to {0}. ", tos);
string filename = HostingEnvironment.MapPath("~/UploadedFiles/0/ElmahMailLog.txt");
StreamWriter file = File.AppendText(filename);
file.WriteLine(msg);
file.Close();
}
protected void Session_Start(object sender, EventArgs e)
{
string msg = "Session started";
string filename = HostingEnvironment.MapPath("~/UploadedFiles/0/ElmahMailLog.txt");
StreamWriter file = File.AppendText(filename);
file.WriteLine(msg);
file.Close();
}
protected void Application_Error(object sender, EventArgs e)
{
string msg = "An error occured";
string filename = HostingEnvironment.MapPath("~/UploadedFiles/0/ElmahMailLog.txt");
StreamWriter file = File.AppendText(filename);
file.WriteLine(msg);
file.Close();
}
Then I went to a page that does not exist to generate a 404 error and my text file on my local machine resulted in this:
Session started
An error occured
Se开发者_如何学编程nding mail to myemail@someplace.com.
Sent mail to myemail@someplace.com.
In addition I received the error email when triggered from my local machine. When I moved this code to a demo site (on the live webserver) though the log file is empty and the email does not get sent. The error does get logged to the Elmah database though.
The only thing I can think of is that the IIS settings must not be correct. Can anybody think of a setting that would prevent elmah from sending emails (but not other parts of my application) and/or would prevent writing to a file? FYI the site does have file uploads that work just fine.
I'm really at a loss here as to what would cause these issues. Any answers or ideas for debugging would be appreciated. Let me know if you need clarification on anything.
EDIT: It looks like my Global.asax file just isn't doing anything at all. Like the event's aren't getting called for some reason. Even the asp.net events.
Are you using a different web.config file for your local and production environments? I usually find that if my local is working but my live is not then my configuration is at fault. If the live site is successfully sending non-ELMAH emails, then I would suggest that it is the ELMAH configuration in the live web.config.
Check you have added the relevant key into the section of the config if you are using IIS6.
Also if you are running IIS7 on your live server, you will need to add the ELMAH key to the system.webServer/modules section of the web.config file.
精彩评论