-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartup.cs
More file actions
81 lines (68 loc) · 2.65 KB
/
Startup.cs
File metadata and controls
81 lines (68 loc) · 2.65 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
using System.IO;
using System.Net;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using MongoDB.Driver;
using Serilog;
using Serilog.Events;
using Serilog.Exceptions;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
namespace StorageToMongo
{
public class Startup
{
public ServiceProvider ServiceCollection { get; }
private IConfiguration Configuration { get; }
public Startup()
{
// configure application
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
#if DEBUG
builder.AddUserSecrets<Startup>();
#endif
Configuration = builder.Build();
// configure DI
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
ServiceCollection = serviceCollection.BuildServiceProvider();
}
private void ConfigureServices(ServiceCollection serviceCollection)
{
// add configuration
serviceCollection.AddSingleton(Configuration);
// add logging
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.WithExceptionDetails()
.WriteTo.Debug()
.WriteTo.Console()
.WriteTo.File("migration.log", LogEventLevel.Information)
.CreateLogger();
serviceCollection
.AddLogging(builder => builder
.AddSerilog()
.SetMinimumLevel(LogLevel.Debug));
// add DB access
// TODO: switch to config file use
serviceCollection
.AddSingleton(provider =>
{
var account = CloudStorageAccount.Parse(Configuration["ConnectionStrings:TableStorage"]);
var tableServicePoint = ServicePointManager.FindServicePoint(account.TableEndpoint);
tableServicePoint.UseNagleAlgorithm = false;
return account.CreateCloudTableClient();
}).AddSingleton(provider =>
{
var client = new MongoClient(Configuration["ConnectionStrings:Mongo"]);
var db = client.GetDatabase(Configuration["MongoDbName"]);
return db;
});
// add migration service
serviceCollection.AddSingleton<Migration>();
}
}
}