1

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

4

2 回答 2

1

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

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

于 2013-06-21T15:42:47.260 回答
1

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

卸载 NLog.MailKit 包为我解决了这个问题!

只有在使用 NetStandard1.X 时才需要 Nlog.MailKit 包。

于 2018-08-14T11:37:00.650 回答