Reputation: 374
I have a c# program that logs with log4net both on file appender and smtp appender.
I would like to keep always the log file but at the end of the program in my c# code I would like to decide if I have to send the email or not.
The usual level or text filters do not help here since while the program runs I log a lot of stuff that may be interesting or not and I know that only at the end of the process.
Is it possible to "block" the smtp appender by code?
Upvotes: 0
Views: 335
Reputation: 73253
You need to set the Threshold property on the appender to OFF to disable logging, and to your required value otherwise, something like this:
private static void SetEmailLogging(bool enabled)
{
// Assuming there aren't more than 1 SmtpAppenders
var appender = log4net.LogManager.GetRepository()
.GetAppenders()
.OfType<SmtpAppender>()
.SingleOrDefault();
if (appender == null)
{
return;
}
// Set the level you want.
appender.Threshold = enabled ? Level.All : Level.Off;
appender.ActivateOptions();
}
Upvotes: 1