views:

545

answers:

1

I am setting up an SMPTAppender to email log files when there is an error in production code. There are some machines, such as test machines that are local, where I don't want the email sent.

I tried to use the environment variable COMPUTERNAME in a propertyfilter, but this didn't work:

<filter type="log4net.Filter.PropertyFilter">
  <Key value="COMPUTERNAME" />
  <StringToMatch value="myComputerName" />
  <Accept value="false" />
</filter>

I have used ComputerName in a file appender like this:

<file value="${HOMEDRIVE}\\loggingDirectory\\AppLogFile.${COMPUTERNAME}.log" />

This also didn't work (nor did I expect it to):

<filter type="log4net.Filter.PropertyFilter">
  <Key value="${COMPUTERNAME}" />
  <StringToMatch value="myComputerName" />
  <Accept value="false" />
</filter>

Is there a way to use environment variables in a property filter? Other suggestions welcome.

+3  A: 

You are using the wrong Key value. The LoggingEvent.Properties collection is populated with the HostName property, which has the "log4net:HostName" signature.

Your filter should look like this:

<filter type="log4net.Filter.PropertyFilter">
    <Key value="log4net:HostName" />
    <StringToMatch value="computerToExclude" />
    <AcceptOnMatch value="false" />
</filter>

Note also to use AcceptOnMatch, not Accept.

Peter Lillevold