2016-04-29 40 views
1

我正在使用log4net smtp appender在應用程序中發生錯誤時發送電子郵件警報。有沒有辦法在運行時分配電子郵件地址,而不是放入配置文件。配置文件是:如何將電子郵件地址作爲參數從C#代碼傳遞到log4net smtp appender

<appender name="LogSmtpAppender" type="log4net.Appender.SmtpAppender"> 
     <authentication value="Basic" /> 
     <to value="" /> 

我從我的應用程序初始化的記錄爲:

public static readonly log4net.ILog applicationLog = log4net.LogManager.GetLogger("MyApplication"); 

回答

0

你可以得到附加器,然後通過設置屬性更改配置,然後再激活,如新選項:

 // Get the Hierarchy object that organizes the loggers 
     Hierarchy hier = log4net.LogManager.GetRepository() as Hierarchy; 
     var smtpappender = 
       (SmtpAppender)hier.GetAppenders().Where(
        appender => appender.Name.Equals("LogSmtpAppender", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault(); 

      if (smtpappender != null) 
      { 
       // Change your setting here 
       smtpappender.To = "[email protected]" 
       // Activate the options 
       smtpappender.ActivateOptions(); 
      } 
     } 
相關問題