2013-06-20 44 views
1

我試圖建立NLOG發送郵件,並使用我的系統設置(與皮卡目錄)documented hereNLOG郵件配置useSystemNetMailSettings「參數useSystemNetMailSettings不支持MailTarget」

這裏是我的NLOG配置

<nlog internalLogLevel="Trace" internalLogFile="C:\NLogInternal.log" throwExceptions="true" autoReload="true" 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:\backupservice.log.txt" layout="${longdate} ${callsite} ${level} ${message}"/> 
    <target name="console" xsi:type="Console" /> 
    <target xsi:type="EventLog" 
      name="event" 
      layout="${longdate} ${callsite} ${level} ${message}" 
      source="BackupService" 
      eventId="898" 
      log="BackupService" 
       /> 
    <target xsi:type="Mail" 
      name="email" 
      useSystemNetMailSettings="True" 
      layout="${longdate} ${callsite} ${level} ${message}" /> 
</targets> 
<rules> 
    <logger name="*" minlevel="Fatal" writeTo="email" /> 
    <logger name="*" minLevel="Info" writeTo="event" /> 
    <logger name="*" minLevel="Debug" writeTo="console" /> 
</rules> 

這裏是我的郵件設置:

<system.net> 
<mailSettings> 
    <smtp from="[email protected][COMPANY].com" deliveryMethod="SpecifiedPickupDirectory"> 
    <specifiedPickupDirectory pickupDirectoryLocation="C:\testmail\Pickup" /> 
    <network host="mail.[COMPANY].com" password="[PASSWORD]" userName="[EMAIL_ADDRESS]" /> 
    </smtp> 
</mailSettings> 

下面是NLOG的內部記錄輸出:

** SNIP ** 
2013-06-20 17:41:03.8368 Debug Setting 'MailTarget.name' to 'email' 
2013-06-20 17:41:03.8368 Debug Setting 'MailTarget.useSystemNetMailSettings' to 'True' 
2013-06-20 17:41:03.8688 Error Error System.NotSupportedException: Parameter useSystemNetMailSettings not supported on MailTarget 
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object o, String name, String value, ConfigurationItemFactory configurationItemFactory) 
at NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType) 
at NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement) 
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement) 
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String baseDirectory) 
at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String baseDirectory) 
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)... 
2013-06-20 17:41:03.8688 Error ConfigSectionHandler error:  NLog.NLogConfigurationException: Exception occurred when loading configuration from C:\Projects\Fee\WindowsServices\BackupService\BackupService\bin\Debug\BackupService.vshost.exe.Config ---> System.NotSupportedException: Parameter useSystemNetMailSettings not supported on MailTarget 
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object o, String name, String value, ConfigurationItemFactory configurationItemFactory) 
at NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType) 
at NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement) 
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement) 
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String baseDirectory) 
at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String baseDirectory) 
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors) 
--- End of inner exception stack trace --- 
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors) 
at NLog.Config.XmlLoggingConfiguration..ctor(XmlElement element, String fileName) 
at NLog.Config.ConfigSectionHandler.System.Configuration.IConfigurationSectionHandler.Create(Object parent, Object configContext, XmlNode section) 

我不知道我做錯了,或者如果它在NLOG的錯誤。

NB:我都試過useSystemNetMailSettings和UseSystemNetMailSettings。

回答

1

所以我沒有得到NLog的工作特別是通過NLog,但我用了一個工作。 NLOG有"MethodCall" target,我用像這樣:

 <target name="sendmail" xsi:type="MethodCall" className="BackupLib.Email, BackupLib" methodName="Send"> 
    <parameter layout="[email protected][COMPANY].com" /> 
    <parameter layout="[email protected][COMPANY].com" /> 
    <parameter layout="FATAL ERROR: Backup Service on ${machinename}" /> 
    <parameter layout="${longdate} - ${callsite} - ${message}" /> 
    </target> 
</targets> 
<rules> 
    <logger name="*" minLevel="Fatal" writeTo="sendmail" /> 
    <logger name="*" minLevel="Info" writeTo="event" /> 
    <logger name="*" minLevel="Debug" writeTo="console" /> 
</rules> 

這讓我打電話給發送一封電子郵件,我的一個靜態方法(BackupLib.Email.Send())。簡單而有效的因爲我用SMTPClient烘焙到.NET中,它使用我的系統電子郵件設置!

很高興我終於得到了這個。

+0

但是你是如何得到日誌文件的?你發送附件了嗎? – NetSide

+0

這樣做是調用一個方法: BackupLib.Email.SendMail(發件人,收件人,主題,正文)。 From/To/Subject/Body對應中的。 每次發生致命錯誤時都會生成一封電子郵件。 –