2014-12-04 47 views
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中的參數來使用佈局以外的其他類型?

回答

0

我利用MailTarget導出自定義的目標解決這個問題:

[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}" 
    />