Dot-Net

如何通過電子郵件發送使用 NLog 記錄的錯誤?[關閉]

  • January 7, 2012

我第一次使用 NLog,我想出瞭如何寫入文本文件,但現在我想給自己發送一封電子郵件。我假設當您向 NLog 提供 SMTP 憑據時。該程序集呼叫 System.Net.Mail 命名空間並處理髮送電子郵件。如果這是錯誤的,請告訴我。如果您在此之前完成了此操作,我將不勝感激有關您完成發送電子郵件的任何資訊。

下面是我的配置。

   <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
 <targets>
   <!--<target name="logfile" xsi:type="File" fileName="C:\Users\keithb\Desktop\TestLog.txt" />-->
   <target name="Mail" xsi:type="Mail" html="true" subject="Error Received" body="${message}"         
        to="user2@someemaill.com"
        from="user@someemail.com"
        Encoding="UTF8"
        smtpUsername="user@someemail.com"
        enableSsl="False"
        smtpPassword="pa$$word"
        smtpAuthentication="Basic"
        smtpServer="mail.someemail.com"
        smtpPort="25" />
 </targets>
 <rules>
   <!--<logger name="*" minlevel="Debug" writeTo="logfile" />-->
   <logger name="*" level="Error" writeTo="Mail" />
   <logger name="*" level="Fatal" writeTo="Mail" />
 </rules>
</nlog>

我這樣稱呼錯誤

Imports NLog

Public Class HandleGetRouteInfo
   Private Shared logger As Logger = LogManager.GetCurrentClassLogger()

   Public Shared Function GetRouteInfo(ByVal routeInfo As RequestGetRouteInfo) As GetRouteInfoResponse
       'TODO: Check route ID, username, password
       logger.Fatal("User" & routeInfo.UserName & "has entered the GetRouteInfo Method.")
       logger.Error("User" & routeInfo.UserName & "has entered the GetRouteInfo Method.")
       Dim str As String = logger.Name
End Function
End Class

我試圖讓它將錯誤、包含消息以及它通常記錄到文件的內容髮送到我的電子郵件。我知道如何擷取異常並將其通過電子郵件發送給自己,但我認為 NLog 有能力做到這一點。任何包含使用電子郵件功能的簡單範例的教程都可以使用。我發現了很多東西,但無法讓它發揮作用。如果你已經這樣做了,一些範常式式碼或我需要做的其他事情的解釋會有所幫助。我無法弄清楚我做錯了什麼。誰有想法?

將您的編碼從 更改UTF8UTF-8.

假設您的 SMTP 設置中沒有其他錯誤(這通常是未發送消息的原因),它應該可以工作。

引用自:https://stackoverflow.com/questions/8766263