v 1.0.1 - Add Cancellation Token to service
This commit is contained in:
@@ -37,6 +37,14 @@
|
|||||||
"PrivateKeyFile" = "8:"
|
"PrivateKeyFile" = "8:"
|
||||||
"TimeStampServer" = "8:"
|
"TimeStampServer" = "8:"
|
||||||
"InstallerBootstrapper" = "3:2"
|
"InstallerBootstrapper" = "3:2"
|
||||||
|
"BootstrapperCfg:{63ACBE69-63AA-4F98-B2B6-99F9E24495F2}"
|
||||||
|
{
|
||||||
|
"Enabled" = "11:TRUE"
|
||||||
|
"PromptEnabled" = "11:TRUE"
|
||||||
|
"PrerequisitesLocation" = "2:1"
|
||||||
|
"Url" = "8:"
|
||||||
|
"ComponentsUrl" = "8:"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
"Release"
|
"Release"
|
||||||
{
|
{
|
||||||
@@ -156,15 +164,15 @@
|
|||||||
{
|
{
|
||||||
"Name" = "8:Microsoft Visual Studio"
|
"Name" = "8:Microsoft Visual Studio"
|
||||||
"ProductName" = "8:Log 4 Graylog"
|
"ProductName" = "8:Log 4 Graylog"
|
||||||
"ProductCode" = "8:{AE9265EA-F205-4212-9A4A-C0AFD73A2067}"
|
"ProductCode" = "8:{20B40DF7-DE7D-4525-8A5A-70A7553AD142}"
|
||||||
"PackageCode" = "8:{688D543B-C948-42B2-9DBB-282E9C06868B}"
|
"PackageCode" = "8:{8B5226CE-438C-4CA5-AA45-6B913E396AD8}"
|
||||||
"UpgradeCode" = "8:{7CE09C91-3C26-4C8E-BC66-003536E12481}"
|
"UpgradeCode" = "8:{7CE09C91-3C26-4C8E-BC66-003536E12481}"
|
||||||
"AspNetVersion" = "8:4.0.30319.0"
|
"AspNetVersion" = "8:4.0.30319.0"
|
||||||
"RestartWWWService" = "11:FALSE"
|
"RestartWWWService" = "11:FALSE"
|
||||||
"RemovePreviousVersions" = "11:FALSE"
|
"RemovePreviousVersions" = "11:FALSE"
|
||||||
"DetectNewerInstalledVersion" = "11:TRUE"
|
"DetectNewerInstalledVersion" = "11:TRUE"
|
||||||
"InstallAllUsers" = "11:TRUE"
|
"InstallAllUsers" = "11:TRUE"
|
||||||
"ProductVersion" = "8:1.0.0"
|
"ProductVersion" = "8:1.0.1"
|
||||||
"Manufacturer" = "8:PAL s.r.l."
|
"Manufacturer" = "8:PAL s.r.l."
|
||||||
"ARPHELPTELEPHONE" = "8:"
|
"ARPHELPTELEPHONE" = "8:"
|
||||||
"ARPHELPLINK" = "8:"
|
"ARPHELPLINK" = "8:"
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
using System.ServiceProcess;
|
using console_log4graylog.Model;
|
||||||
using console_log4graylog.Model;
|
|
||||||
using console_log4graylog.Workers;
|
using console_log4graylog.Workers;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Core;
|
using Serilog.Core;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using Serilog.Sinks.Graylog;
|
using Serilog.Sinks.Graylog;
|
||||||
|
using System.ServiceProcess;
|
||||||
|
|
||||||
namespace console_log4graylog
|
namespace console_log4graylog
|
||||||
{
|
{
|
||||||
@@ -15,8 +13,6 @@ namespace console_log4graylog
|
|||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
ServiceBase[] servicesToRun;
|
|
||||||
|
|
||||||
IConfigurationRoot config = new ConfigurationBuilder()
|
IConfigurationRoot config = new ConfigurationBuilder()
|
||||||
.AddJsonFile("appsettings.json",
|
.AddJsonFile("appsettings.json",
|
||||||
optional: true,
|
optional: true,
|
||||||
@@ -24,11 +20,6 @@ namespace console_log4graylog
|
|||||||
.AddEnvironmentVariables()
|
.AddEnvironmentVariables()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
servicesToRun = new []
|
|
||||||
{
|
|
||||||
new MainWorker(config)
|
|
||||||
};
|
|
||||||
|
|
||||||
var graylogSettings = config.GetSection("GraylogSettings").Get<GraylogSettings>();
|
var graylogSettings = config.GetSection("GraylogSettings").Get<GraylogSettings>();
|
||||||
|
|
||||||
Log.Logger = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
@@ -41,7 +32,7 @@ namespace console_log4graylog
|
|||||||
Facility = graylogSettings.Facility
|
Facility = graylogSettings.Facility
|
||||||
}).CreateLogger();
|
}).CreateLogger();
|
||||||
|
|
||||||
ServiceBase.Run(servicesToRun);
|
ServiceBase.Run(new MainWorker(config));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
using console_log4graylog.Model;
|
using console_log4graylog.Model;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.FileSystemGlobbing;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
|
|
||||||
@@ -13,8 +10,8 @@ namespace console_log4graylog.Workers
|
|||||||
{
|
{
|
||||||
private SettingsModel Settings { get; set; }
|
private SettingsModel Settings { get; set; }
|
||||||
private FileSystemWatcher watcher;
|
private FileSystemWatcher watcher;
|
||||||
private Thread monitorThread;
|
private Task monitorTask;
|
||||||
private bool running = true;
|
private CancellationTokenSource cancellationTokenSource;
|
||||||
|
|
||||||
public MainWorker(IConfiguration _configuration)
|
public MainWorker(IConfiguration _configuration)
|
||||||
{
|
{
|
||||||
@@ -29,9 +26,9 @@ namespace console_log4graylog.Workers
|
|||||||
Log2File("Start Log 4 Graylog");
|
Log2File("Start Log 4 Graylog");
|
||||||
Log.Logger.Warning("▶️ Start Log 4 Graylog on " + Dns.GetHostName());
|
Log.Logger.Warning("▶️ Start Log 4 Graylog on " + Dns.GetHostName());
|
||||||
|
|
||||||
|
cancellationTokenSource = new CancellationTokenSource();
|
||||||
|
monitorTask = Task.Run(() => MonitorFolder(cancellationTokenSource.Token), cancellationTokenSource.Token);
|
||||||
|
|
||||||
monitorThread = new Thread(MonitorFolder);
|
|
||||||
monitorThread.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnStop()
|
protected override void OnStop()
|
||||||
@@ -39,23 +36,28 @@ namespace console_log4graylog.Workers
|
|||||||
Log2File("End Log 4 Graylog");
|
Log2File("End Log 4 Graylog");
|
||||||
Log.Logger.Error("⏹️ End Log 4 Graylog on " + Dns.GetHostName());
|
Log.Logger.Error("⏹️ End Log 4 Graylog on " + Dns.GetHostName());
|
||||||
|
|
||||||
running = false;
|
cancellationTokenSource.Cancel();
|
||||||
watcher?.Dispose();
|
watcher?.Dispose();
|
||||||
|
monitorTask?.Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MonitorFolder()
|
private async Task MonitorFolder(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
while (running)
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var latestFile = GetLatestLogFile();
|
var latestFile = GetLatestLogFile();
|
||||||
if (latestFile != null)
|
if (latestFile != null)
|
||||||
{
|
{
|
||||||
MonitorLogFile(latestFile);
|
await MonitorLogFile(latestFile, cancellationToken);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await Task.Delay(50, cancellationToken);
|
||||||
}
|
}
|
||||||
Thread.Sleep(1000);
|
|
||||||
}
|
}
|
||||||
|
catch (TaskCanceledException) { return; }
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LogError("Service encountered an error: " + ex.Message);
|
LogError("Service encountered an error: " + ex.Message);
|
||||||
@@ -71,7 +73,7 @@ namespace console_log4graylog.Workers
|
|||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MonitorLogFile(FileInfo file)
|
private async Task MonitorLogFile(FileInfo file, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -81,8 +83,10 @@ namespace console_log4graylog.Workers
|
|||||||
using (var reader = new StreamReader(fs))
|
using (var reader = new StreamReader(fs))
|
||||||
{
|
{
|
||||||
reader.BaseStream.Seek(0, SeekOrigin.End);
|
reader.BaseStream.Seek(0, SeekOrigin.End);
|
||||||
while (running)
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
string line = reader.ReadLine();
|
string line = reader.ReadLine();
|
||||||
while (line != null)
|
while (line != null)
|
||||||
{
|
{
|
||||||
@@ -93,10 +97,11 @@ namespace console_log4graylog.Workers
|
|||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Thread.Sleep(500);
|
await Task.Delay(1, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (TaskCanceledException) { return; }
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LogError("Error monitoring file: " + ex.Message);
|
LogError("Error monitoring file: " + ex.Message);
|
||||||
|
|||||||
@@ -7,6 +7,6 @@
|
|||||||
"GeneralSettings" : {
|
"GeneralSettings" : {
|
||||||
"DirPath" : "C:\\Logs",
|
"DirPath" : "C:\\Logs",
|
||||||
"FileNamePattern": "*.log",
|
"FileNamePattern": "*.log",
|
||||||
"LogPath": "C:\\Logs\\l4g_debug.log"
|
"LogPath": "C:\\Logs\\debug.l4g"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user