2012-09-06 25 views
0

我正試圖修復一個遠程服務器上的腳本,該腳本應該使用SmtpClient()發送電子郵件。不幸的是,它失敗了,並且有一個通用的catch-all異常,它向視圖輸出一個錯誤消息,然後在它的途中快樂地傳遞。如何記錄捕獲的ASP.NET MVC3異常文件?

我反而想做的事,因爲我不能真正地遠程調試它(並且我似乎無法在本地產生問題),有異常是爲我記錄到一個文件通過FTP(我知道,共享主機)下載並仔細閱讀以瞭解發生了什麼。

下面是代碼:

try 
{ 
    var client = new SmtpClient(); 

    var emailRecipients = new List<string> { "[email protected]" }; 

    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."); 
} 
+1

只需使用log4net的? –

+0

你有什麼麻煩?你在問關於日誌框架還是在哪裏登錄? –

+6

[ELMAH](http://code.google.com/p/elmah/)是你的朋友。獲取MVC3 NuGet包以方便使用! – Oded

回答

1

下載Log4net,並添加引用到您的項目。

Configure a rolling file appender

現在,在您想要記錄錯誤的項目中使用此代碼。


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