How to Use the Log4Net StringMatchFilter

How to Use the Log4Net StringMatchFilter

Log4Net is a very powerful, easy to use logging tool, so it’s no surprise that so many .NET developers use it in their applications. One under documented (at least in my opinion) feature of it, is the use of filters to be more specific about what you want your appenders to filter out.

Recently, in an application I was working on, we wanted to only log specific messages that had certain strings present. The StringMatchFilter can do exactly this. Here is a sample configuration, using that filter:

<filter type="log4net.Filter.StringMatchFilter">
 <stringToMatch value="some string we want to match" />
 <filter type="log4net.Filter.StringMatchFilter">
 <stringToMatch value="another string that we could match" />
 <filter type="log4net.Filter.StringMatchFilter">
 <stringToMatch value="one last string to match" />
 <filter type="log4net.Filter.DenyAllFilter" />

This filter, goes inside the configuration for whatever Log4Net appender you’re using. There are some things to note here that could possibly trip you up:

  1. Filters are automatically evaluated as “OR”. From everything I’ve read, and understand, if you want to evaluate multiple filters in an “AND” fashion, you would need to write custom filters to accomplish this. This is not the default Log4Net behavior.
  2. In this case the DenyAllFilter tells Log4Net that if none of the strings are matched, we don’t log anything. The default behavior is to log everything that makes it through the OR conditions.
  3. Filters are evaluated in order, and thus, this is why the DenyAllFilter goes at the bottom. If a log message makes it that far without a match, we don’t watch to log it. In this case at least…
  4. The StringMatchFilter will match on partial strings, but is case-sensitive.

There are multiple other filters that Log4Net provides by default, that may come in handy, such as a level-range filter. Hopefully this blog post gives you a little more information on how Log4Net is actually evaluating these filters.

Share Your Thoughts!


What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!


I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at or drop me a line using the button below:

Copyright © 2018 Krueger Web Design - All rights reserved
Web Design in Madison, WI