Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
auto-tx
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vamp
auto-tx
Commits
ecf825ac
Commit
ecf825ac
authored
7 years ago
by
Niko Ehrenfeuchter
Browse files
Options
Downloads
Patches
Plain Diff
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
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ATXCommon/ATXCommon.csproj
+1
-0
1 addition, 0 deletions
ATXCommon/ATXCommon.csproj
ATXCommon/NLog/RateLimitWrapper.cs
+34
-0
34 additions, 0 deletions
ATXCommon/NLog/RateLimitWrapper.cs
AutoTx/AutoTx.cs
+16
-4
16 additions, 4 deletions
AutoTx/AutoTx.cs
with
51 additions
and
4 deletions
ATXCommon/ATXCommon.csproj
+
1
−
0
View file @
ecf825ac
...
...
@@ -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"
/>
...
...
This diff is collapsed.
Click to expand it.
ATXCommon/NLog/RateLimitWrapper.cs
0 → 100644
+
34
−
0
View file @
ecf825ac
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
;
}
}
}
This diff is collapsed.
Click to expand it.
AutoTx/AutoTx.cs
+
16
−
4
View file @
ecf825ac
...
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment