1 using Microsoft.Extensions.Logging;
2 using Microsoft.Extensions.Logging.EventLog;
3 using Microsoft.Extensions.Logging.EventLog.Internal;
6 using System.Diagnostics.CodeAnalysis;
8 using System.ServiceProcess;
10 using System.Threading.Tasks;
23 public const string Name =
"tgstation-server-4";
48 if (watchdogFactory == null)
49 throw new ArgumentNullException(nameof(watchdogFactory));
50 if (loggerFactory == null)
51 throw new ArgumentNullException(nameof(loggerFactory));
53 loggerFactory.AddEventLog(
new EventLogSettings
56 Filter = (message, logLevel) => logLevel >= minumumLogLevel
64 public int MaxMessageSize => (int)EventLog.MaximumKilobytes * 1024;
67 public void WriteEntry(
string message, EventLogEntryType type,
int eventID,
short category) => EventLog.WriteEntry(message, type, eventID, category);
75 async Task
RunWatchdog(
string[] args, CancellationToken cancellationToken)
77 await watchdog.RunAsync(
false, args, cancellationToken).ConfigureAwait(
false);
79 void StopServiceAsync()
83 Task.Run(Stop, cancellationToken);
85 catch (OperationCanceledException) { }
88 EventLog.WriteEntry(String.Format(CultureInfo.InvariantCulture,
"Error stopping service! Exception: {0}", e));
95 [SuppressMessage(
"Microsoft.Usage",
"CA2213:DisposableFieldsShouldBeDisposed", MessageId =
"cancellationTokenSource")]
96 protected override void Dispose(
bool disposing)
98 cancellationTokenSource?.Dispose();
99 base.Dispose(disposing);
103 protected override void OnStart(
string[] args)
105 cancellationTokenSource?.Dispose();
106 cancellationTokenSource =
new CancellationTokenSource();
107 watchdogTask = RunWatchdog(args, cancellationTokenSource.Token);
113 cancellationTokenSource.Cancel();
114 watchdogTask.GetAwaiter().GetResult();
async Task RunWatchdog(string[] args, CancellationToken cancellationToken)
Executes the watchdog, stopping the service if it exits
IWatchdog CreateWatchdog(ILoggerFactory loggerFactory)
Create a IWatchdog
Use server authentication
override void OnStart(string[] args)
Task watchdogTask
The Task recieved from IWatchdog.RunAsync(bool, string[], CancellationToken) of watchdog ...
override void Dispose(bool disposing)
ServerService(IWatchdogFactory watchdogFactory, ILoggerFactory loggerFactory, LogLevel minumumLogLevel)
Construct a ServerService
Factory for creating IWatchdogs
readonly IWatchdog watchdog
The IWatchdog for the ServerService
Represents a IWatchdog as a ServiceBase
CancellationTokenSource cancellationTokenSource
The cancellationTokenSource for the ServerService