-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
100 lines (87 loc) · 3.67 KB
/
Program.cs
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
namespace GranadaCoder.OrganizaionApp.ConsoleOne
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GranadaCoder.Organizaion.Domain;
using GranadaCoder.OrganizaionApp.Bal.Managers;
using GranadaCoder.OrganizaionApp.Bal.Managers.Interfaces;
using GranadaCoder.OrganizationApp.Dal;
using GranadaCoder.OrganizationApp.Dal.Interfaces;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
public class Program
{
public static async Task<int> Main(string[] args)
{
Logger lgr = LogManager.GetCurrentClassLogger();
try
{
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
IServiceProvider servicesProvider = BuildDi(config);
using (servicesProvider as IDisposable)
{
IEmployeeManager empMan = servicesProvider.GetRequiredService<IEmployeeManager>();
empMan.DoSomething();
Employee emp = await empMan.GetByID(int.MaxValue);
ShowEmployee(lgr, "GetByID", emp);
ICollection<Employee> emps = await empMan.GetByDateOfBirth(DateTime.MaxValue);
ShowEmployeeCollection(lgr, "GetByDateOfBirth", emps);
Console.WriteLine("Press ANY key to exit");
Console.ReadLine();
}
}
catch (Exception ex)
{
// NLog: catch any exception and log it.
lgr.Error(ex, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
LogManager.Shutdown();
}
Console.WriteLine("Returning 0 and exiting.");
return 0;
}
private static void ShowEmployee(Logger lgr, string label, Employee emp)
{
ICollection<Employee> emps = new List<Employee> { emp };
ShowEmployeeCollection(lgr, label, emps);
}
private static void ShowEmployeeCollection(Logger lgr, string label, ICollection<Employee> emps)
{
if (null != emps)
{
foreach (Employee emp in emps)
{
string msg = string.Format("{0} ::: {1},{2},{3},{4}", label, emp.ID, emp.FirstName, emp.LastName, emp.DateOfBirth);
lgr.Info(msg);
}
}
}
private static IServiceProvider BuildDi(IConfiguration config)
{
string connectionString = config.GetConnectionString("MyConnectionString");
return new ServiceCollection()
.AddSingleton<IEmployeeManager, EmployeeManager>()
.AddTransient<IEmployeeDataLayer, EmployeeDataLayer>()
.AddLogging(loggingBuilder =>
{
// configure Logging with NLog
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
loggingBuilder.AddNLog(config);
})
.AddSingleton<IConfiguration>(config)
.BuildServiceProvider();
}
}
}