Wednesday, July 5, 2017

log4net string pattern

After a few years I had to define some log4net appenders in config files and I couldn't remember how to substitute runtime values into the appender parameters. A classic need was to change the address of the mail server for an SmtpAppender. I spent about 30 minutes searching for an answer and came across all sorts of stupid or outdated suggestions that required tedious coding. I knew there was an easy way, but it was so easy I couldn't remember it! Then I finally found it half way down the official FAQ page.

Set the required values at runtime:

log4net.GlobalContext.Properties["mailHost"] = /*something*/

Use a corresponding property in the config file:

<smtpHost type="log4net.Util.PatternString" value="%property{mailHost}"/>

No comments:

Post a Comment