Faruk
Faruk

Reputation: 11

How to use LOG4NET for specific events

Hi just learning Log4net and i am stuck at something

I am using Info(...) for write log files, but I also need to use Info(...) for email in specific events.

So one call to Info(...) writes to the log and and sends an email.

Upvotes: 0

Views: 159

Answers (2)

Peter
Peter

Reputation: 27944

You can add an extra logger for your specific logger in you log4net config:

<log4net>
  <appender name="DefaultAppender" type="...">
    ...
  </appender>
  <appender name="MailAppender" type="...">
    ...
  </appender>
  <root>
    <level value="INFO" />
    <appender-ref ref="DefaultAppender" />
  </root>
  <logger name="MyLoggerNameMail">
    <level value="INFO" />
   <appender-ref ref="MailAppender" />
  </logger>
</log4net>

Upvotes: 1

stuartd
stuartd

Reputation: 73303

Probably the best way to accomplish this would be to add a filter to your SMTP appender: a StringMatchFilter is probably your best bet.

There is an example of using a StringMatchFilter with an SMTPAppender at http://maonet.wordpress.com/2012/01/06/use-log4net-filter-to-dispatch-email-notification-based-on-string-match

The author notes:

  • filter type set to log4net.Filter.StringMatchFilter
  • each filter section can only have one stringToMatch element
  • end with a “log4net.Filter.DenyAllFilter” filter to switch from the default “accept all unless instructed otherwise” filtering behavior to a “deny all unless instructed otherwise” behavior.

Upvotes: 1

Related Questions