【发布时间】: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