NLog邮件配置useSystemNetMailSettings " MailTarget"不支持参

本文关键字:quot MailTarget 不支持 useSystemNetMailSettings NLog 配置 | 更新日期: 2023-09-27 18:19:22

我正在尝试设置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的一个bug。

注意:我已经尝试了useSystemNetMailSettings和useSystemNetMailSettings .

NLog邮件配置useSystemNetMailSettings " MailTarget"不支持参

所以我没有让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()),它为我发送一封电子邮件。简单而有效的,因为我使用了。net中的SMTPClient,它使用了我的系统电子邮件设置!

很高兴我终于得到了这个

如果您有扩展名NLog。MailKit安装后,您尝试在目标中使用useSystemNetMailSettings参数,这将导致错误Parameter useSystemNetMailSettings not supported on MailTarget

卸载NLog。MailKit包为我修复了这个问题!

Nlog。MailKit包只有在使用netstandard . x时才需要。