Charley Rathkopf
Charley Rathkopf

Reputation: 4788

Configure Log4Net to not use smtp appender if running an a certain machine

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.

Upvotes: 5

Views: 1202

Answers (1)

Peter Lillevold
Peter Lillevold

Reputation: 33920

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.

Upvotes: 5

Related Questions