Using log4net to Send Mail With SmtpAppender In a Console Application
I recently built some console apps to do some testing on my server. I wanted to be able to run the apps and get them to print info to the console. I also wanted it to log to a text file and to send e-mails to me. Log4net was the perfect tool to get this done.
This is how my .config files look with log4net. Of course, you have to reference log4net and so on also, use NuGet!
<configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" /> </configSections> [..] <log4net> <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net"> <file value="webcontent-logfile.txt" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message% - %level%newline" /> </layout> </appender> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender, log4net"> <mapping> <level value="ERROR" /> <foreColor value="Red, HighIntensity" /> </mapping> <mapping> <level value="INFO" /> <foreColor value="White" /> </mapping> <mapping> <level value="DEBUG" /> <foreColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level - %message% - %newline" /> </layout> </appender> <appender name="EmailAppender" type="log4net.Appender.SmtpAppender"> <!-- The SmtpAppender authenticates against the mail server, the buffersize of 10 provides 10 lines of context when an error happens. --> <subject value="Log: Web Content Server Report" /> <to value="[email protected]" /> <from value="[email protected]" /> <smtpHost value="localhost" /> <bufferSize value="100000" /> <lossy value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level - %message% - %newline" /> </layout> </appender> <root> <appender-ref ref="FileAppender"/> <appender-ref ref="ColoredConsoleAppender"/> <appender-ref ref="EmailAppender"/> </root> </log4net> </configuration>
In the application, I just init log4net like this.
log4net.Config.XmlConfigurator.Configure(); ILog log = log4net.LogManager.GetLogger(typeof(Program));
I saw loads of examples where they used the “BasicConfigurator” instead of the “XmlConfigurator” for some reason but that did not work if I wanted to use the appenders specified in the .config files.
Anyway, since my console app is just running and then quits I had some problems with the SmtpAppender. I guessed I didn’t have time to send the mail before I exited the app but I found some code to solve this as well. Add this at the end of the app and you should be fine.
ILoggerRepository rep = LogManager.GetRepository(); foreach (IAppender appender in rep.GetAppenders()) { var buffered = appender as BufferingAppenderSkeleton; if (buffered != null) { buffered.Flush(); } }
Another trick is if you want to change the subject of the e-mail sent you can do like this.
ILoggerRepository rep = LogManager.GetRepository(); foreach (IAppender appender in rep.GetAppenders()) { if (appender is SmtpAppender) { ((SmtpAppender)appender).Subject = "Log: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm") +" Web Content Server Report"; } var buffered = appender as BufferingAppenderSkeleton; if (buffered != null) { buffered.Flush(); } }
Sorry for not linking to the sources of all this code but most is just stolen from various Stack Overflow questions.