【问题标题】:nlog renderer for types other than the Layout (for target attributes)布局以外的类型的 nlog 渲染器(用于目标属性)
【发布时间】:2023-04-09 03:16:01
【问题描述】:

我正在尝试使用自定义渲染器通过 app.config 中的参数配置邮件目标。我的 NLog 邮件目标:

<target name="AlertMail" xsi:type="Mail"
  to="${mailTo}"
  from="${mailFrom}"
  html="true"
  encoding="${enc}"
  subject="[APPNAME][${mailEnv}][${instanceName}][${logger:uppercase=true}]"
  smtpServer="${smtpHost}"
  smtpAuthentication="${smtpAuth}"
  smtpUserName="${smtpUserName}"
  smtpPassword="${smtpPassword}"
  header="${mailHtmlHeader}${mailBodyHeader}"
  layout="${mailMessage}"
  footer="${mailBodyFooter}${mailHtmlFooter}"/>

NLog 变量:

<variable name="${enc}" value="${app-settings:default.encoding}" />
<variable name="${smtpAuth}" value="${app-settings:alerting.smtp.authentication}" />

NLog 布局渲染器:

[LayoutRenderer("app-settings")]
public class AppSettingsRenderer : LayoutRenderer
{
    [RequiredParameter]
    [DefaultParameter]
    public string Key { get; set; }
    protected override void Append(StringBuilder builder, NLog.LogEventInfo logEvent)
    {
        var param = ConfigurationManager.AppSettings[this.Key];
        builder.Append(param);
    }
}

我遇到了错误

Error when setting property 'Encoding' on Mail Target[AlertMail]
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)
at NLog.Config.XmlLoggingConfiguration..ctor(String fileName)
at Common.Logging.NLog.NLogLoggerFactoryAdapter..ctor(NameValueCollection properties) in D:\Project\GSJSJZB\GSJ_SJZB\Common.Logging.NLog\Logging\NLog\NLogLoggerFactoryAdapter.cs:line 109

这显然是因为 LayoutRenderer 处理的属性只有 Layout 类型。属性 smtpAuthentication 有同样的问题。如何解决此限制以将 appconfig 中的参数用于 Layout 以外的类型?

【问题讨论】:

    标签: layout config nlog renderer


    【解决方案1】:

    我使用派生自 MailTarget 的自定义 Target 解决了这个问题:

    [Target("MyMail")]
    class MyMailTarget : MailTarget
    {
        /// <summary>
        /// Wrapper for enum to use placeholders
        /// </summary>
        [DefaultValue("None")]
        public Layout SmtpAuth { get; set; }
    
        protected override void Write(NLog.Common.AsyncLogEventInfo logEvent)
        {
            Write(new[] {logEvent});
        }
    
        protected override void Write(NLog.Common.AsyncLogEventInfo[] logEvents)
        {
            var renderedAuthType = SmtpAuth.Render(logEvents.Last().LogEvent);
            SmtpAuthenticationMode mode;
            if (Enum.TryParse(renderedAuthType, true, out mode))
                SmtpAuthentication = mode;
            else
            {
                InternalLogger.Warn("Could not convert '{0}' to SmtpAuthenticationMode, valid values are None, Basic and Ntlm.");
                throw new NLogRuntimeException(String.Format("Cannot render SmtpAuthentication. SmtpAuth = {0}", renderedAuthType));
            }
            base.Write(logEvents);
        }
    }
    

    使用:

    <extensions>
      <add assembly="AssemplyNameWithMyMailTarget" />
    </extensions>
    
    <variable name="smtpAuth" value="${app-settings:alerting.SMTP.Authentication}"/>
    
    <!-- smtpAuth - my custom attribute -->
    
    <target name="AlertMail" xsi:type="MyMail"
      to="${mailTo}"
      from="${mailFrom}"
      subject="[APPNAME][ALERT][${mailEnv}][${instanceName}]"
      smtpServer="${smtpHost}"
      smtpAuth="${smtpAuth}" 
      smtpUserName="${smtpUserName}"
      smtpPassword="${smtpPassword}"
      layout="${longdate}${newline}${message}${newline}"
      />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多