Elmah 电子邮件发送和 File.AppendText 在实时服务器上不起作用

发布于 2024-10-11 05:36:09 字数 2651 浏览 3 评论 0原文

我最近在我正在开发的网站上设置了 Elmah。实际的日志记录和 elmah 视图页面在我的本地服务器和实时服务器上均有效。 Elmah 电子邮件发送功能可以在我的本地服务器上运行,但不能在实时服务器上运行。我环顾四周,找不到这个问题的任何解决方案,但我确实找到了一篇文章,其中有人建议向 Global.asax 添加一些代码,以便将 elmah 事件记录到文件中以尝试和调试它。根据该建议,我创建了一个 global.asax 文件(该站点以前没有该文件)并添加了以下代码:

    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();
    }

然后我转到一个不存在的页面以生成 404 错误,并且本地计算机上的文本文件导致了此错误:

Session started
An error occured
Sending mail to [email protected]. 
Sent mail to [email protected]. 

此外,当我的本地计算机触发时,我收到了错误电子邮件。当我将此代码移至演示站点(在实时网络服务器上)时,尽管日志文件为空并且电子邮件未发送。不过,该错误确实被记录到 Elmah 数据库中。

我唯一能想到的就是IIS设置一定不正确。有人能想到一种设置可以阻止 elmah 发送电子邮件(但不能阻止我的应用程序的其他部分)和/或阻止写入文件吗?仅供参考,该网站确实有可以正常工作的文件上传功能。

我真的不知道什么会导致这些问题。任何调试的答案或想法将不胜感激。如果您需要澄清任何事情,请告诉我。

编辑:看起来我的 Global.asax 文件根本没有做任何事情。就像由于某种原因该事件没有被调用一样。甚至是 asp.net 事件。

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
Sending mail to [email protected]. 
Sent mail to [email protected]. 

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-10-18 05:36:09

您是否为本地和生产环境使用不同的 web.config 文件?我通常发现,如果我的本地工作正常,但我的现场工作不工作,那么我的配置就有问题。如果实时站点成功发送非 ELMAH 电子邮件,那么我建议它是实时 web.config 中的 ELMAH 配置。

如果您使用的是 IIS6,请检查您是否已将相关密钥添加到配置部分。

此外,如果您在实时服务器上运行 IIS7,则需要将 ELMAH 密钥添加到 web.config 文件的 system.webServer/modules 部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文