Last active
February 28, 2020 07:02
-
-
Save AlexanderByndyu/5538568 to your computer and use it in GitHub Desktop.
log4net, custom SmtpAppender with Subject layout. Download NuGet: https://nuget.org/packages/log4net.Appender.SmtpAppenderWithSubjectLayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CustomSmtpAppender : SmtpAppender | |
| { | |
| public PatternLayout SubjectLayout { get; set; } | |
| protected override void SendBuffer(LoggingEvent[] events) | |
| { | |
| PrepareSubject(events); | |
| base.SendBuffer(events); | |
| } | |
| protected virtual void PrepareSubject(IEnumerable<LoggingEvent> events) | |
| { | |
| Subject = string.Empty; | |
| foreach (LoggingEvent @event in events) | |
| { | |
| if (Evaluator.IsTriggeringEvent(@event)) | |
| Subject += SubjectLayout.Format(@event); | |
| } | |
| if (string.IsNullOrEmpty(Subject)) | |
| throw new InvalidOperationException("Необходимо указать Subject"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <log4net> | |
| <root> | |
| <level value="Info" /> | |
| <appender-ref ref="SMTPAppender" /> | |
| </root> | |
| <appender name="SMTPAppender" type="CustomSmtpAppender, AppenderAssembly"> | |
| <bufferSize value="1"/> | |
| <authentication value="Basic" /> | |
| <to value="[email protected]" /> | |
| <from value="[email protected]" /> | |
| <subjectLayout> | |
| <conversionPattern value="Some message from service // %p: %date [%c]" /> | |
| </subjectLayout> | |
| <smtpHost value="servername" /> | |
| <port value="25" /> | |
| <username value="username" /> | |
| <password value="password" /> | |
| <lossy value="true" /> | |
| <evaluator type="log4net.Core.LevelEvaluator"> | |
| <threshold value="ERROR" /> | |
| </evaluator> | |
| <layout type="log4net.Layout.PatternLayout"> | |
| <conversionPattern value="%property{log4net:HostName}%newline%date [%thread] %-5level %logger %newline %appdomain %newline %message%newline%newline%newline" /> | |
| </layout> | |
| </appender> | |
| </log4net> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment