NLog邮件配置使用SystemNetMailSettings“MailTarget不支持参数useSystemNetMailSettings”

时间:2013-06-20 21:56:13

标签: c# .net configuration nlog

我正在尝试设置NLog以发送邮件并使用我的系统设置(带有取件目录)documented here

这是我的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。

2 个答案:

答案 0 :(得分:1)

所以我没有让NLog通过NLog 专门工作,但我使用了一个解决方法。 NLog有一个"MethodCall" target我就这样使用了:{/ p>

      <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,它使用了我的系统电子邮件设置!

很高兴我终于得到了这个。

答案 1 :(得分:1)

如果安装了扩展名NLog.MailKit,并且尝试在目标中使用useSystemNetMailSettings参数,则将导致错误Parameter useSystemNetMailSettings not supported on MailTarget

卸载NLog.MailKit软件包可以为我解决这个问题!

仅在使用NetStandard1.X时才需要Nlog.MailKit程序包。