Skip to content

Commit

Permalink
Merge pull request #9 from MattMofDoom/dev
Browse files Browse the repository at this point in the history
v1.4.0
  • Loading branch information
MattMofDoom authored Jun 14, 2021
2 parents 95607e6 + a5baebe commit 4aed18e
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Globalization;
using System.Linq;

namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Classes
{
/// <summary>
/// AbstractAPI Holidays API format
Expand Down Expand Up @@ -31,50 +31,51 @@ protected AbstractApiHolidays(string name, string name_local, string language, s
string location, string type, string date, string date_year, string date_month, string date_day,
string week_day)
{
Name = name;
Name_local = name_local;
Language = language;
Description = description;
Location = location;
if (Location.Contains(" - "))
Locations = Location
.Substring(Location.IndexOf(" - ", StringComparison.Ordinal) + 3,
Location.Length - Location.IndexOf(" - ", StringComparison.Ordinal) - 3)
this.name = name;
this.name_local = name_local;
this.language = language;
this.description = description;
this.location = location;
if (this.location.Contains(" - "))
Locations = this.location
.Substring(this.location.IndexOf(" - ", StringComparison.Ordinal) + 3,
this.location.Length - this.location.IndexOf(" - ", StringComparison.Ordinal) - 3)
.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToList();
else
Locations = Location.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim())
Locations = this.location.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim())
.ToList();

Country = country;
Type = type;
Date = date;
Date_year = date_year;
Date_month = date_month;
Date_day = date_day;
Week_day = week_day;
LocalStart = DateTime.ParseExact(Date, "M/d/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
this.country = country;
this.type = type;
this.date = date;
this.date_year = date_year;
this.date_month = date_month;
this.date_day = date_day;
this.week_day = week_day;
LocalStart = DateTime.ParseExact(this.date, "M/d/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
UtcStart = LocalStart.ToUniversalTime();
UtcEnd = LocalStart.AddDays(1).ToUniversalTime();
}

// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local
// ReSharper disable UnusedAutoPropertyAccessor.Global
public string Name { get; }
public string Name_local { get; }
public string Language { get; }
public string Description { get; }
public string Country { get; }
public string Location { get; }
public string name { get; }
public string name_local { get; }
public string language { get; }
public string description { get; }
public string country { get; }
public string location { get; }
public List<string> Locations { get; }
public string Type { get; }
public string type { get; }
public DateTime LocalStart { get; }
public DateTime UtcStart { get; }
public DateTime UtcEnd { get; }
public string Date { get; }
public string Date_year { get; }
public string Date_month { get; }
public string Date_day { get; }
public string Week_day { get; }
public string date { get; }
public string date_year { get; }
public string date_month { get; }
public string date_day { get; }
public string week_day { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Net.Http;
using Flurl.Http.Configuration;

namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Classes
{
/// <summary>
/// HTTP Client for retrieving Holidays API
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Seq.App.EventTimeout.Enums;

namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Classes
{
/// <summary>
/// Date Expression comprising the day order, day type, day of week, and day of month
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Seq.App.EventTimeout.Enums;

namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Classes
{
/// <summary>
/// Date calculations for inclusions/exclusions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Globalization;
using System.Linq;

namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Classes
{
/// <summary>
/// Holidays methods
Expand Down Expand Up @@ -48,7 +48,7 @@ public static List<AbstractApiHolidays> ValidateHolidays(IEnumerable<AbstractApi
if (holidayMatch.Count > 0)
// ReSharper disable once UnusedVariable
foreach (var match in holidayMatch.Where(match =>
holiday.Type.IndexOf(match, StringComparison.OrdinalIgnoreCase) >= 0))
holiday.type.IndexOf(match, StringComparison.OrdinalIgnoreCase) >= 0))
hasType = true;

if (localeMatch.Count > 0)
Expand All @@ -58,7 +58,7 @@ public static List<AbstractApiHolidays> ValidateHolidays(IEnumerable<AbstractApi
0))
hasRegion = true;

if (!includeBank && holiday.Name.IndexOf("Bank Holiday", StringComparison.OrdinalIgnoreCase) >= 0)
if (!includeBank && holiday.name.IndexOf("Bank Holiday", StringComparison.OrdinalIgnoreCase) >= 0)
isBank = true;

if (!includeWeekends && (holiday.LocalStart.DayOfWeek == DayOfWeek.Sunday ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Classes
{
/// <summary>
/// Property matching methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading.Tasks;
using Flurl.Http;

namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Classes
{
/// <summary>
/// Web access methods
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Enums
{
/// <summary>
/// Match days of week, or none
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Enums
{
/// <summary>
/// Day order within a date expression
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Seq.App.EventTimeout
namespace Seq.App.EventTimeout.Enums
{
/// <summary>
/// Day type within a date expression
Expand Down
89 changes: 58 additions & 31 deletions Seq.App.EventTimeout/EventTimeoutReactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.Linq;
using System.Timers;
using Seq.App.EventTimeout.Classes;
using Seq.Apps;
using Seq.Apps.LogEvents;

Expand Down Expand Up @@ -49,11 +50,13 @@ public class EventTimeoutReactor : SeqApp, ISubscribeTo<LogEventData>

// Count of matches
private int _matched;
private string _priority;
private Dictionary<string, string> _properties;
private string _proxy;
private string _proxyPass;
private string _proxyUser;
private bool _repeatTimeout;
private string _responders;
private int _retryCount;
private bool _skippedShowtime;
private string _startFormat = "H:mm:ss";
Expand All @@ -65,15 +68,14 @@ public class EventTimeoutReactor : SeqApp, ISubscribeTo<LogEventData>
private LogEventLevel _timeoutLogLevel;
private Timer _timer;
private bool _useHolidays;
private bool _useProxy;

// ReSharper disable MemberCanBePrivate.Global
private bool _useProxy; // ReSharper disable MemberCanBePrivate.Global

// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
[SeqAppSetting(
DisplayName = "Diagnostic logging",
HelpText = "Send extra diagnostic logging to the stream.")]
public bool Diagnostics { get; set; }
HelpText = "Send extra diagnostic logging to the stream. Enabled by default.")]
public bool Diagnostics { get; set; } = true;

[SeqAppSetting(
DisplayName = "Start time",
Expand All @@ -87,16 +89,39 @@ public class EventTimeoutReactor : SeqApp, ISubscribeTo<LogEventData>

[SeqAppSetting(
DisplayName = "Timeout interval (seconds)",
HelpText = "Time period in which a matching log entry must be seen. After this, an alert will be raised.",
HelpText =
"Time period in which a matching log entry must be seen. After this, an alert will be raised. Default 60.",
InputType = SettingInputType.Integer)]
public int Timeout { get; set; }
public int Timeout { get; set; } = 60;

[SeqAppSetting(
DisplayName = "Repeat timeout",
HelpText =
"Optionally re-arm the timeout after a match, within the start/end time parameters - useful for a 'heartbeat' style alert.",
"Optionally re-arm the timeout after a match, within the start/end time parameters - useful for a 'heartbeat' style alert. Disabled by default.",
IsOptional = true)]
public bool RepeatTimeout { get; set; } = false;

[SeqAppSetting(
DisplayName = "Suppression interval (seconds)",
HelpText =
"If an alert has been raised, further alerts will be suppressed for this time. Will also suppress repeating timeout matches. Default 60.",
InputType = SettingInputType.Integer)]
public int SuppressionTime { get; set; } = 60;

[SeqAppSetting(DisplayName = "Log level for timeouts",
HelpText = "Verbose, Debug, Information, Warning, Error, Fatal. Defaults to Error.",
IsOptional = true)]
public string TimeoutLogLevel { get; set; }

[SeqAppSetting(DisplayName = "Priority for timeouts",
HelpText = "Optional Priority property to pass for timeouts, for use with other apps.",
IsOptional = true)]
public bool RepeatTimeout { get; set; }
public string Priority { get; set; }

[SeqAppSetting(DisplayName = "Responders for timeouts",
HelpText = "Optional Responders property to pass for timeouts, for use with other apps.",
IsOptional = true)]
public string Responders { get; set; }

[SeqAppSetting(
DisplayName = "Days of week",
Expand All @@ -117,17 +142,6 @@ public class EventTimeoutReactor : SeqApp, ISubscribeTo<LogEventData>
IsOptional = true)]
public string ExcludeDaysOfMonth { get; set; }

[SeqAppSetting(
DisplayName = "Suppression interval (seconds)",
HelpText =
"If an alert has been raised, further alerts will be suppressed for this time. Will also suppress repeating timeout matches.",
InputType = SettingInputType.Integer)]
public int SuppressionTime { get; set; }

[SeqAppSetting(DisplayName = "Log level for timeouts",
HelpText = "Verbose, Debug, Information, Warning, Error, Fatal.",
IsOptional = true)]
public string TimeoutLogLevel { get; set; }

[SeqAppSetting(
DisplayName = "Property 1 name",
Expand Down Expand Up @@ -211,7 +225,7 @@ public class EventTimeoutReactor : SeqApp, ISubscribeTo<LogEventData>
[SeqAppSetting(
DisplayName = "Holidays - use Holidays API for public holiday detection",
HelpText = "Connect to the AbstractApi Holidays service to detect public holidays.")]
public bool UseHolidays { get; set; }
public bool UseHolidays { get; set; } = false;

[SeqAppSetting(
DisplayName = "Holidays - Retry count",
Expand Down Expand Up @@ -272,7 +286,7 @@ public class EventTimeoutReactor : SeqApp, ISubscribeTo<LogEventData>
[SeqAppSetting(
DisplayName = "Holidays - proxy bypass local addresses",
HelpText = "Bypass local addresses for proxy.")]
public bool BypassLocal { get; set; }
public bool BypassLocal { get; set; } = true;

[SeqAppSetting(
DisplayName = "Holidays - local addresses for proxy bypass",
Expand Down Expand Up @@ -485,7 +499,7 @@ protected override void OnAttached()
LogEvent(LogEventLevel.Debug, "Validate Alert Description '{AlertDescription}' ...", AlertDescription);

_alertDescription = string.IsNullOrWhiteSpace(AlertDescription)
? _alertMessage + " : Generated by Seq " + Host.BaseUri
? ""
: AlertDescription.Trim();
if (_diagnostics)
LogEvent(LogEventLevel.Debug, "Alert Description '{AlertDescription}' will be used ...",
Expand All @@ -502,6 +516,12 @@ protected override void OnAttached()
if (string.IsNullOrWhiteSpace(TimeoutLogLevel)) TimeoutLogLevel = "Error";
if (!Enum.TryParse(TimeoutLogLevel, out _timeoutLogLevel)) _timeoutLogLevel = LogEventLevel.Error;

if (!string.IsNullOrEmpty(Priority))
_priority = Priority;

if (!string.IsNullOrEmpty(Responders))
_responders = Responders;

if (_diagnostics)
LogEvent(LogEventLevel.Debug, "Log level {loglevel} will be used for timeouts on {Instance} ...",
_timeoutLogLevel, App.Title);
Expand Down Expand Up @@ -569,8 +589,10 @@ private void TimerOnElapsed(object sender, ElapsedEventArgs e)
var suppressDiff = timeNow - _lastLog;
if (_isAlert && suppressDiff.TotalSeconds < _suppressionTime.TotalSeconds) return;

//Log event
LogEvent(_timeoutLogLevel, "{Message} : {Description}", _alertMessage, _alertDescription);
//Log event
LogEvent(_timeoutLogLevel,
string.IsNullOrEmpty(_alertDescription) ? "{Message}" : "{Message} : {Description}",
_alertMessage, _alertDescription);
_lastLog = timeNow;
_isAlert = true;
}
Expand Down Expand Up @@ -794,17 +816,17 @@ private void RetrieveHolidays(DateTime localDate, DateTime utcDate)
foreach (var holiday in result)
LogEvent(LogEventLevel.Debug,
"Holiday Name: {Name}, Local Name {LocalName}, Start {LocalStart}, Start UTC {Start}, End UTC {End}, Type {Type}, Location string {Location}, Locations parsed {Locations} ...",
holiday.Name, holiday.Name_local, holiday.LocalStart, holiday.UtcStart, holiday.UtcEnd,
holiday.Type, holiday.Location, holiday.Locations.ToArray());
holiday.name, holiday.name_local, holiday.LocalStart, holiday.UtcStart, holiday.UtcEnd,
holiday.type, holiday.location, holiday.Locations.ToArray());
}

LogEvent(LogEventLevel.Debug, "Holidays retrieved and validated {holidayCount} ...",
_holidays.Count);
foreach (var holiday in _holidays)
LogEvent(LogEventLevel.Debug,
"Holiday Name: {Name}, Local Name {LocalName}, Start {LocalStart}, Start UTC {Start}, End UTC {End}, Type {Type}, Location string {Location}, Locations parsed {Locations} ...",
holiday.Name, holiday.Name_local, holiday.LocalStart, holiday.UtcStart, holiday.UtcEnd,
holiday.Type, holiday.Location, holiday.Locations.ToArray());
holiday.name, holiday.name_local, holiday.LocalStart, holiday.UtcStart, holiday.UtcEnd,
holiday.type, holiday.location, holiday.Locations.ToArray());

_isUpdating = false;
if (!_isShowtime) UtcRollover(utcDate, true);
Expand Down Expand Up @@ -893,9 +915,12 @@ private void LogEvent(LogEventLevel logLevel, string message, params object[] ar

if (_isTags)
Log.ForContext(nameof(Tags), _tags).ForContext("AppName", App.Title)
.ForContext(nameof(Priority), _priority).ForContext(nameof(Responders), _responders)
.Write((Serilog.Events.LogEventLevel) logLevel, message, logArgs);
else
Log.ForContext("AppName", App.Title).Write((Serilog.Events.LogEventLevel) logLevel, message, logArgs);
Log.ForContext("AppName", App.Title).ForContext(nameof(Priority), _priority)
.ForContext(nameof(Responders), _responders)
.Write((Serilog.Events.LogEventLevel) logLevel, message, logArgs);
}

/// <summary>
Expand All @@ -919,9 +944,11 @@ private void LogEvent(LogEventLevel logLevel, Exception exception, string messag

if (_isTags)
Log.ForContext(nameof(Tags), _tags).ForContext("AppName", App.Title)
.ForContext(nameof(Priority), _priority).ForContext(nameof(Responders), _responders)
.Write((Serilog.Events.LogEventLevel) logLevel, exception, message, logArgs);
else
Log.ForContext("AppName", App.Title)
Log.ForContext("AppName", App.Title).ForContext(nameof(Priority), _priority)
.ForContext(nameof(Responders), _responders)
.Write((Serilog.Events.LogEventLevel) logLevel, exception, message, logArgs);
}
}
Expand Down
Loading

0 comments on commit 4aed18e

Please sign in to comment.