Skip to content
Snippets Groups Projects
Commit ecf825ac authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Wrap mail targets for NLog by a rate-limiting wrapper.

To avoid flooding recipients and / or SMTP servers.

Refers to #3, #4
parent fc3ab5e5
No related branches found
No related tags found
No related merge requests found
......@@ -49,6 +49,7 @@
<Compile Include="ActiveDirectory.cs" />
<Compile Include="Conv.cs" />
<Compile Include="FsUtils.cs" />
<Compile Include="NLog\RateLimitWrapper.cs" />
<Compile Include="Serializables\DriveToCheck.cs" />
<Compile Include="Serializables\ServiceConfig.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
......
using System;
using System.ComponentModel;
using NLog.Common;
using NLog.Targets;
using NLog.Targets.Wrappers;
namespace ATXCommon.NLog
{
/// <summary>
/// A wrapper target for NLog, limiting the rate of messages being logged.
///
/// Meant to be used in conjunction with the MailTarget class, to avoid flooding the recipient
/// with too many emails and probably being banned by the SMTP server for spamming.
/// NOTE: should always be used in combination with another target (FileTarget) to ensure that
/// all messages are being logged, including those ones discarded by *this* target.
/// </summary>
[Target("FrequencyWrapper", IsWrapper = true)]
public class RateLimitWrapper : WrapperTargetBase
{
private DateTime _lastLogEvent = DateTime.MinValue;
protected override void Write(AsyncLogEventInfo logEvent) {
if ((DateTime.Now - _lastLogEvent).TotalMinutes >= MinLogInterval) {
_lastLogEvent = DateTime.Now;
WrappedTarget.WriteAsyncLogEvent(logEvent);
} else {
logEvent.Continuation(null);
}
}
[DefaultValue(30)]
public int MinLogInterval { get; set; }
}
}
......@@ -10,6 +10,7 @@ using NLog;
using NLog.Config;
using NLog.Targets;
using ATXCommon;
using ATXCommon.NLog;
using ATXCommon.Serializables;
using RoboSharp;
......@@ -120,6 +121,8 @@ namespace AutoTx
_config.HostAlias, Environment.MachineName, LogFormatDefault);
var logConfig = LogManager.Configuration;
// "Fatal" target
var mailTargetFatal = new MailTarget {
Name = "mailfatal",
SmtpServer = _config.SmtpHost,
......@@ -129,9 +132,14 @@ namespace AutoTx
Subject = subject,
Body = body,
};
logConfig.AddTarget(mailTargetFatal);
logConfig.AddRuleForOneLevel(LogLevel.Fatal, mailTargetFatal);
var mailTargetFatalLimited = new RateLimitWrapper {
Name = "mailfatallimited",
WrappedTarget = mailTargetFatal
};
logConfig.AddTarget(mailTargetFatalLimited);
logConfig.AddRuleForOneLevel(LogLevel.Fatal, mailTargetFatalLimited);
// "Error" target
if (!string.IsNullOrWhiteSpace(_config.AdminDebugEmailAdress)) {
var mailTargetError = new MailTarget {
Name = "mailerror",
......@@ -142,8 +150,12 @@ namespace AutoTx
Subject = subject,
Body = body,
};
logConfig.AddTarget(mailTargetError);
logConfig.AddRuleForOneLevel(LogLevel.Error, mailTargetError);
var mailTargetErrorLimited = new RateLimitWrapper {
Name = "mailerrorlimited",
WrappedTarget = mailTargetError
};
logConfig.AddTarget(mailTargetErrorLimited);
logConfig.AddRuleForOneLevel(LogLevel.Error, mailTargetErrorLimited);
Log.Info("Configured mail notification for 'Error' messages to {0}", mailTargetError.To);
}
Log.Info("Configured mail notification for 'Fatal' messages to {0}", mailTargetFatal.To);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment