Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit 20d6279

Browse files
committed
Set the application name to "PVBridge Service" and create an event log source named like that.
Remove code that copies CreateDefaultBuilder() behavior.
1 parent a8ce33d commit 20d6279

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/CodeCaster.PVBridge.Service/CommandLine/WindowsServiceManager.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ internal class WindowsServiceManager
1414
{
1515
private readonly ILogger<WindowsServiceManager> _logger;
1616

17+
/// <summary>
18+
/// Event Log name.
19+
/// </summary>
20+
private const string applicationLogName = "Application";
21+
1722
// TODO: validate setter
1823
private string ServiceName { get; } = "PVBridge";
1924

@@ -54,6 +59,23 @@ internal async Task InstallServiceAsync(IServiceProvider services, string servic
5459
throw new InvalidOperationException($"The service path \"{servicePath}\" does not point to a valid executable file.");
5560
}
5661

62+
// Create the event log source so we can log warnings and up to the event log.
63+
try
64+
{
65+
if (!EventLog.SourceExists(Program.ApplicationName))
66+
{
67+
_logger.LogInformation("Creating {application} log source {source}", applicationLogName, Program.ApplicationName);
68+
69+
EventLog.CreateEventSource(Program.ApplicationName, applicationLogName);
70+
}
71+
}
72+
catch (Exception e)
73+
{
74+
// That's not a fatal error.
75+
_logger.LogError(e, "Creating {application} log source {source} failed", applicationLogName, Program.ApplicationName);
76+
}
77+
78+
5779
using (var serviceController = GetServiceController())
5880
{
5981
if (serviceController == null)
@@ -189,7 +211,7 @@ private async Task<int> InstallServiceAsync(string servicePath)
189211

190212
// Set failure mode: retry twice after 2 minutes, then after a day. Error count resets in a day.
191213
scArguments = $"failure {ServiceName} reset= 86400 actions= restart/60000/restart/60000/restart/86400";
192-
214+
193215
return await RunCreateServiceAsync(scArguments);
194216
}
195217

src/CodeCaster.PVBridge.Service/Program.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace CodeCaster.PVBridge.Service
2121
/// </summary>
2222
public static class Program
2323
{
24+
/// <summary>
25+
/// Event Log source name.
26+
/// </summary>
27+
public static readonly string ApplicationName = "PVBridge Service";
28+
2429
public static async Task Main(string[] args)
2530
{
2631
try
@@ -61,7 +66,7 @@ private static Command GetSyncCommand(IHost host)
6166
};
6267

6368
// TODO: make Argument<DateTime?>, but that doesn't work: https://github.com/dotnet/command-line-api/issues/1669
64-
var untilOption = new Option<DateTime?>(new [] { "--until", "-u" }, "The inclusive end date to sync. Format: yyyy-MM-dd.")
69+
var untilOption = new Option<DateTime?>(new[] { "--until", "-u" }, "The inclusive end date to sync. Format: yyyy-MM-dd.")
6570
{
6671
IsRequired = false
6772
};
@@ -204,19 +209,13 @@ private static IHostBuilder CreateHostBuilder(string[] args)
204209
return Host.CreateDefaultBuilder(args)
205210
.ConfigureAppConfiguration((context, config) =>
206211
{
207-
config.AddEnvironmentVariables();
208-
209-
// enviroment from command line
210-
// e.g.: dotnet run --environment "Staging"
211-
config.AddCommandLine(args);
212-
213-
config.AddJsonFile("appsettings.json");
214-
config.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true);
212+
// Sets the Event Log source name (through UseWindowsServiceExtensions -> UseWindowsService).
213+
context.HostingEnvironment.ApplicationName = Program.ApplicationName;
215214

215+
// Read "C:\ProgramData\PVBridge\PVBridge.AccountConfig.json". Optional because it doesn't exist on first run.
216216
var globalSettingsFilePath = ConfigurationReader.GlobalSettingsFilePath;
217217
var globalSettingsFileName = Path.GetFileNameWithoutExtension(ConfigurationReader.GlobalSettingsFilePath);
218218

219-
// Read "C:\ProgramData\PVBridge\PVBridge.AccountConfig.json". Optional because it doesn't exist on first run.
220219
config.AddJsonFile(ConfigurationReader.GlobalSettingsFilePath, optional: true, reloadOnChange: true);
221220
})
222221
.ConfigureServices((context, services) =>

0 commit comments

Comments
 (0)