1
我想通過使用自定義渲染器的app.config中的params配置郵件目標。我NLOG郵件目標: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處理屬性只有佈局類型。屬性smtpAuthentication具有相同的問題。如何解決此限制使用appconfig中的參數來使用佈局以外的其他類型?