Skip to content

Commit

Permalink
Add detection and recovery for no logs being seen for a configurable …
Browse files Browse the repository at this point in the history
…number of heartbeats
  • Loading branch information
MattMofDoom committed Feb 27, 2022
1 parent 7260bc2 commit 7832593
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Seq.Client.EventLog/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<add key="LogFolder" value="" />
<!-- Heartbeat log entry interval in seconds, 0 disables heartbeat, default 600, maximum 86399 -->
<add key="HeartbeatInterval" value="600" />
<!-- If the Event Log service doesn't see any new log activity for the below number of heartbeats, it can restart the listener to recover-->
<add key="HeartbeatsBeforeReset" value="2"/>
<!-- Set IsDebug to true for additional heartbeat logging -->
<add key="IsDebug" value="false" />
</appSettings>
Expand Down
5 changes: 5 additions & 0 deletions src/Seq.Client.EventLog/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static Config()
LogToFile = GetBool(ConfigurationManager.AppSettings["LogSeqApiKey"], true);
LogFolder = ConfigurationManager.AppSettings["LogFolder"];
HeartbeatInterval = GetInt(ConfigurationManager.AppSettings["HeartbeatInterval"]);
HeartbeatsBeforeReset = GetInt(ConfigurationManager.AppSettings["HeartbeatsBeforeReset"]);

//Minimum is 0 (disabled)
if (HeartbeatInterval < 0)
Expand All @@ -23,6 +24,9 @@ static Config()
if (HeartbeatInterval > 3600)
HeartbeatInterval = 3600;

if (HeartbeatsBeforeReset < 0)
HeartbeatsBeforeReset = 0;

IsDebug = GetBool(ConfigurationManager.AppSettings["IsDebug"]);

var isSuccess = true;
Expand Down Expand Up @@ -65,6 +69,7 @@ static Config()
public static string LogFolder { get; }
public static int HeartbeatInterval { get; }
public static bool IsDebug { get; }
public static int HeartbeatsBeforeReset { get; }

/// <summary>
/// Convert the supplied <see cref="object" /> to an <see cref="int" />
Expand Down
2 changes: 1 addition & 1 deletion src/Seq.Client.EventLog/Seq.Client.EventLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</PropertyGroup>
<PropertyGroup>
<StartupObject />
<Version>3.0.3</Version>
<Version>3.0.4</Version>
<Authors>Connor O'Shea and contributors</Authors>
<Copyright>Connor O'Shea</Copyright>
<ApplicationIcon>EventLog.ico</ApplicationIcon>
Expand Down
24 changes: 24 additions & 0 deletions src/Seq.Client.EventLog/ServiceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Lurgle.Logging;
using Newtonsoft.Json;
using Timer = System.Timers.Timer;

// ReSharper disable MemberCanBePrivate.Global

Expand Down Expand Up @@ -33,6 +36,7 @@ public static class ServiceManager
public static string JsonConfigPath { get; set; }
public static List<EventLogListener> EventLogListeners { get; set; }

private static int ZeroLogHeartbeats { get; set; } = 0;

public static void Start(bool isInteractive)
{
Expand Down Expand Up @@ -145,6 +149,26 @@ private static void ServiceHeartbeat(object sender, ElapsedEventArgs e)
serviceCounters.Add("NonInteractiveLogons", NonInteractiveLogons);
}

if (Config.HeartbeatsBeforeReset > 0)
{
if (EventsProcessed > LastProcessed)
ZeroLogHeartbeats = 0;
else
ZeroLogHeartbeats++;

if (ZeroLogHeartbeats > Config.HeartbeatsBeforeReset)
{
Log.Warning().AddProperty("HeartbeatName", $"{Config.AppName} Heartbeat")
.AddProperty("HeartbeatCount", ZeroLogHeartbeats)
.AddProperty("NextTime", timeNow.AddMilliseconds(_isInteractive ? 60000 : Config.HeartbeatInterval * 1000))
.Add("[{HeartbeatName:l} - {MachineName:l}] - Detected that new no log entries have been seen in the past {Heartbeats} heartbeats, resetting listeners ...");

StopListeners();
Thread.Sleep(1000);
StartListeners(_isInteractive);
}
}

Log.Debug()
.AddProperty("HeartbeatName", $"{Config.AppName} Heartbeat")
.AddProperty(serviceCounters)
Expand Down

0 comments on commit 7832593

Please sign in to comment.