简体   繁体   English

如何将捕获到的ASP.NET MVC3异常记录到文件?

[英]How do I log caught ASP.NET MVC3 exception to file?

I am trying to fix a script on a remote server that is supposed to be sending e-mails with SmtpClient(). 我正在尝试在应该使用SmtpClient()发送电子邮件的远程服务器上修复脚本。 Unfortunately, it fails, and there's a generic catch-all exception that outputs an error message to the view and then just carries merrily on it's way. 不幸的是,它失败了,并且有一个通用的包罗万象的异常,该异常将错误消息输出到视图,然后随心所欲地进行下去。

What I instead want to do, because I can't really remotely debug it (and I don't seem to be able to produce the issue locally), is have the exception that is thrown logged to a file for me to download via FTP (I know, shared hosts) and peruse in order to figure out what's going on. 由于我无法真正对其进行远程调试(而且我似乎无法在本地产生问题),因此我想做的是,有一个例外,该例外被记录到文件中,以供我通过FTP下载(我知道,共享主机)并仔细研究以便了解发生了什么。

Here is the code: 这是代码:

try
{
    var client = new SmtpClient();

    var emailRecipients = new List<string> { "test@example.co.uk" };

    var message = new MailMessage(Sender, emailRecipients.First());

    Person recipient = null;
    if (cbr.RecipientId.HasValue)
    {
        recipient = (from p in Entities.People where p.Id == cbr.RecipientId.Value select p).FirstOrDefault();
    }

    message.Subject = "Callback Request from " + cbr.Name;
    message.Body = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\r\n\r\n" + "A callback request by \"" + cbr.Name + "\" has been made from the Foreman Laws website";

    if (!string.IsNullOrWhiteSpace(cbr.SourceURL))
    {
        message.Body += ", from the following URL: " + cbr.SourceURL + "\r\n\r\n";
    }
    else
    {
        message.Body += ".\r\n\r\n";
    }

    if (cbr.DepartmentId.HasValue)
    {
        var department = (from d in Entities.CustomRouteItems where d.Id == cbr.DepartmentId.Value select d).FirstOrDefault() as CustomPage;
        if (department != null)
        {
            message.Body += "Request for someone in " + department.Name + " to call back.\r\n";
            String email;
            if (DepartmentEmails.TryGetValue(department.Name, out email))
            {
                message.CC.Add(email);
            }
        }
        else
        {
            message.Body += "Request for anyone to call back (no department selected).\r\n";
        }
    }
    else if (recipient != null)
    {
        message.Body += "Request for " + recipient.Name + " to call back.\r\n";
        if (!string.IsNullOrWhiteSpace(recipient.Email))
        {
            message.CC.Add(recipient.Email);
        }
    } 
    else
    {
        message.Body += "Request for anyone to call back (no department selected).\r\n";
    }

    if (recipient != null)
    {
        message.Body += "The request was made from " + recipient.Name + "'s profile page on the site.\r\n\r\n";
    }

    message.Body += cbr.Name + "'s phone number is: " + cbr.TelephoneNumber + "\r\n";
    if (!string.IsNullOrWhiteSpace(cbr.Email))
    {
        message.Body += cbr.Name + "'s email address is " + cbr.Email + "\r\n";
    }

    message.Body += "\r\n";

    if (!string.IsNullOrWhiteSpace(cbr.SpecificTime))
    {
        message.Body += "The following time was specified for the callback: " + cbr.SpecificTime + "\r\n\r\n";
    }

    client.Send(message);

    cbr.Sent = true;
}
catch (Exception e)
{
    ModelState.AddModelError("Sent", "Sorry, your request could not be sent at this time, please try again later.");
}

Download Log4net and add reference to your project. 下载Log4net并添加对您的项目的引用。

Configure a rolling file appender . 配置滚动文件追加器

Now use this code in your project where you want to log the error. 现在,在您要记录错误的项目中使用此代码。


using log4net;
Class YourClassName
{
    static readonly ILog _log = LogManager.GetLogger(typeof(YourClassName));
    public void DoWork()
    {
        try
        {
             //Your code goes here [written in your question]
        }
        catch(Exception e)
        {
            _log.Fatal(e);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM