根據不同環境讀取對應的設定,動態改變custom target的參數
在nlog.config custom target中加入自訂attribute “WebHookUrl“,並使用layout renderer串接appsettings
<target name ="mySlack" xsi:type="Slack" WebHookUrl="${configsetting:item=NLogSlack.WebHookUrl}"></target>
在custom target程式中接收自訂的attribute,並在寫入log時轉為appsettings中對應的值
[Target("Slack")]
public class SlackTarget : AsyncTaskTarget
{
//接收nlog.config中自訂的attribute
[RequiredParameter]
public Layout WebHookUrl { get; set; }
protected override Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken cancellationToken)
{
//將Layout renderer轉為實際的值
var webHookUrl = WebHookUrl.Render(logEvent);
//do something...
}
}