-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (105 loc) · 4.14 KB
/
Copy pathProgram.cs
File metadata and controls
125 lines (105 loc) · 4.14 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// ***********************************************************************
// Assembly : DataExtractNetCore5
// Author : Leo Mrozek
// Created : 09-17-2020
//
// Last Modified By : Leo Mrozek
// Last Modified On : 11-29-2021
// ***********************************************************************
// <copyright file="Program.cs" company="Leo Mrozek Consulting">
// Leo Mrozek
// </copyright>
// <summary></summary>
// ***********************************************************************
using CommandLine;
using DataExtract.Classes;
using DataExtract.Interfaces;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
namespace DataExtract
{
/// <summary>
/// Class Program.
/// </summary>
internal class Program
{
/// <summary>
/// Static variable that stores information from Configuration file
/// </summary>
private static IConfiguration _configuration;
/// <summary>
/// Starting point of the application.
/// </summary>
/// <param name="args">Command Line Arguments: Currently only WaitForExit and ForceReload</param>
private static void Main(string[] args)
{
// Get the system configuration
_configuration = GetConfiguration();
var services = ConfigureServices();
// Create service collection and configure our services
var serviceProvider = services.BuildServiceProvider();
// Create console logger
serviceProvider
.GetRequiredService<ILoggerFactory>()
.CreateLogger<ConsoleApplication>();
// Kick off our actual code
Parser.Default.ParseArguments<CommandLineOptions>(args)
.WithParsed(opts => { serviceProvider.GetService<ConsoleApplication>()?.Run(opts); });
Environment.Exit(0);
}
/// <summary>
/// Loads the various classes into the application using DI.
/// </summary>
/// <returns>IServiceCollection.</returns>
private static IServiceCollection ConfigureServices()
{
// Get LogLevel from Configuration
var logLevel = new LoggingInfo(_configuration).GetMinLogLevel();
var services = new ServiceCollection();
services.AddLogging(loggingBuilder => loggingBuilder
.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
//options.TimestampFormat = "MM/dd/yyyy HH:mm:ss ";
options.TimestampFormat = "HH:mm:ss ";
})
.AddConsole()
.SetMinimumLevel(logLevel));
services.AddSingleton(_configuration);
services.AddSingleton<IDalService, DalService>();
services.AddSingleton<IDal, Dal>();
services.AddSingleton<ConsoleApplication>();
return services;
}
/// <summary>
/// Loads the configuration and sets the application environment.
/// </summary>
/// <returns>IConfigurationRoot.</returns>
public static IConfigurationRoot GetConfiguration()
{
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var json = $"{Directory.GetCurrentDirectory()}\\appsettings.{env}.json";
if (env == null) { json = $"{Directory.GetCurrentDirectory()}\\appsettings.json"; }
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory)?.FullName)
.AddJsonFile(json, false)
.AddEnvironmentVariables()
.AddConfiguration(new ConfigurationRoot(new List<IConfigurationProvider>()))
.Build();
var appSettings = new AppSettings(configuration);
configuration.Bind("AppSettings", appSettings);
var connectionStrings = new ConnectionStrings(configuration);
configuration.Bind("ConnectionStrings", connectionStrings);
// Used when you need to see what is being loaded from the appsettings.json file
//configuration.GetSection("LoggingInfo").GetChildren().ToList().ForEach(l => { Console.WriteLine($"Path: {l.Path}, Value: { l.Value}"); });
//configuration.GetSection("AppSettings").GetChildren().ToList().ForEach(l => { Console.WriteLine($"Path: {l.Path}, Value: { l.Value}"); });
//configuration.GetSection("ConnectionStrings").GetChildren().ToList().ForEach(l => { Console.WriteLine($"Path: {l.Path}, Value: { l.Value}"); });
return configuration;
}
}
}