-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (97 loc) · 3.76 KB
/
Copy pathProgram.cs
File metadata and controls
105 lines (97 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using ReachOutVeevaPromoMats.Configurations;
using System;
using System.IO;
using System.Threading.Tasks;
namespace ReachOutVeevaPromoMats
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static async Task Main(string[] args)
{
try
{
// Build configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
// Configure NLog from configuration
LogManager.Setup().LoadConfigurationFromFile("NLog.config");
// Check if running interactively or as a service
if (Environment.UserInteractive)
{
// Running interactively - run once and exit
Console.WriteLine("Running in interactive mode...");
var appConfig = new AppConfiguration(configuration);
var service = new VeevaBackgroundService(
new ConsoleLogger<VeevaBackgroundService>(),
appConfig);
service.DoWork();
Console.WriteLine("Work completed. Press any key to exit.");
Console.ReadKey();
}
else
{
// Running as a Windows Service
var host = CreateHostBuilder(args, configuration).Build();
await host.RunAsync();
}
}
catch (Exception ex)
{
Console.WriteLine($"Fatal error: {ex}");
Environment.Exit(1);
}
}
/// <summary>
/// Creates the host builder for the Windows Service.
/// </summary>
static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration)
{
return Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "ReachOutVeevaPromoMats";
})
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton(configuration);
services.AddSingleton<AppConfiguration>();
services.AddHostedService<VeevaBackgroundService>();
})
.ConfigureLogging((context, logging) =>
{
logging.ClearProviders();
logging.AddNLog();
})
.UseConsoleLifetime();
}
}
/// <summary>
/// Simple console logger for interactive mode
/// </summary>
public class ConsoleLogger<T> : ILogger<T>
{
public IDisposable BeginScope<TState>(TState state) => null!;
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) => true;
public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
Console.WriteLine($"[{logLevel}] {formatter(state, exception)}");
if (exception != null)
{
Console.WriteLine(exception.ToString());
}
}
}
}