簡體   English   中英

NLog郵件配置useSystemNetMailSettings“ MailTarget不支持參數useSystemNetMailSettings”

[英]NLog Mail Configuration useSystemNetMailSettings “Parameter useSystemNetMailSettings not supported on MailTarget”

我正在嘗試將NLog設置為發送郵件並使用此處記錄的我的系統設置(帶有提取目錄)

這是我的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="backup@[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中的錯誤。

注意:我已經嘗試了useSystemNetMailSettings和UseSystemNetMailSettings。

所以我沒有讓NLog 專門通過NLog工作,但是我使用了一種變通方法。 NLog有一個我這樣使用的“ MethodCall”目標

      <target name="sendmail" xsi:type="MethodCall" className="BackupLib.Email, BackupLib" methodName="Send">
    <parameter layout="backupservice@[COMPANY].com" />
    <parameter layout="backups@[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中,因此它使用了我的系統電子郵件設置!

很高興我終於明白了。

如果安裝了擴展名NLog.MailKit,並且嘗試在目標中使用useSystemNetMailSettings參數,則會導致Parameter useSystemNetMailSettings not supported on MailTarget錯誤Parameter useSystemNetMailSettings not supported on MailTarget

卸載NLog.MailKit軟件包對我來說已經解決了!

僅在使用NetStandard1.X時才需要Nlog.MailKit程序包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM