2017-08-14 120 views
0

我正在使用NLog,而且我在記錄方面很新穎。我創建了日誌文件,但不知怎的,我有一個問題發送電子郵件。我遵循所有說明,但無法完成。在配置標籤如何使用NLog發送電子郵件?

郵件設置在web.config中:

<system.net> 
<mailSettings> 
    <smtp deliveryMethod="SpecifiedPickupDirectory" from="[email protected]"> 
    <network host="localhost"/> 
    <specifiedPickupDirectory pickupDirectoryLocation="d:\tmp\email"/> 
    </smtp> 

</mailSettings> 
</system.net> 

這是nlog.config內部的目標:

<target name="Mail" xsi:type="Mail" html="true" subject="Error Received" 
body="${message}" 
to="[email protected]" 
from="[email protected]" 
encoding="UTF-8" 
smtpUserName="[email protected]" 
enableSsl="false" 
smtpPassword="password" 
smtpAuthentication="Basic" 
smtpServer="smtp.some.com" 
smtpPort="25" /> 

我使用的規則:

 <logger name="*" minlevel="Error" writeTo="Mail" /> 

我打電話給這樣的記錄器:

 Logger logger = LogManager.GetCurrentClassLogger(); 
    try{ //something } 
    catch(Exception ex){ logger.Error(ex); } 

而且我對設置和配置的地方感到很困惑。謝謝。

回答

1

正文是佈局格式,不知道它是否會處理$ {message}標籤。將$ {message]更改爲nlog.config中的佈局名稱,或者將它保留爲$ {message} $ {newline}的默認值。

您可以在Internal Debugging

更改頂部XML父轉向

<nlog internalLogFile="c:\log.txt" internalLogLevel="Trace"> 

這可能給你對這個問題的想法。可能是服務器或其他問題的身份驗證問題。

由於您正在指定目標中的所有服務器信息,因此不需要web.config設置。

不確定它是否已修復,但是您應該向目標添加timeout =「10000」,以便關閉連接。

對於Gmail的NLog Wiki有一個很好的例子

+0

非常感謝。我嘗試了你說過的話,我仍然在努力。我想這是一個身份驗證問題。 –

+0

然後檢查內部日誌。請參閱https://github.com/NLog/NLog/wiki/Internal-logging – Julian