Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 451
Charley Rathkopf Avatar asked Mar 06 '09 20:03

Charley Rathkopf


1 Answers

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.

like image 51
Peter Lillevold Avatar answered Sep 21 '22 19:09

Peter Lillevold