forked from dotnet/try
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
161 lines (136 loc) · 5.69 KB
/
Copy pathProgram.cs
File metadata and controls
161 lines (136 loc) · 5.69 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using MLS.Agent.Tools;
using Pocket;
using Pocket.For.ApplicationInsights;
using Recipes;
using Serilog.Sinks.RollingFileAlternate;
using System;
using System.CommandLine.Parsing;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using static Pocket.Logger<MLS.Agent.Program>;
using SerilogLoggerConfiguration = Serilog.LoggerConfiguration;
using MLS.Agent.CommandLine;
using WorkspaceServer.Servers;
namespace MLS.Agent
{
public class Program
{
private static readonly ServiceCollection _serviceCollection = new ServiceCollection();
public static async Task<int> Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
return await CommandLineParser.Create( _serviceCollection ).InvokeAsync(args);
}
public static X509Certificate2 ParseKey(string base64EncodedKey)
{
var bytes = Convert.FromBase64String(base64EncodedKey);
return new X509Certificate2(bytes);
}
private static readonly Assembly[] _assembliesEmittingPocketLoggerLogs = {
typeof(Startup).Assembly,
typeof(AsyncLazy<>).Assembly,
typeof(IWorkspaceServer).Assembly
};
private static IDisposable StartAppInsightsLogging(StartupOptions options)
{
var disposables = new CompositeDisposable();
if (options.Production)
{
var applicationVersion = VersionSensor.Version().AssemblyInformationalVersion;
var websiteSiteName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") ?? "UNKNOWN-AGENT";
var regionId = options.RegionId ?? "undefined";
disposables.Add(
LogEvents.Enrich(a =>
{
a(("regionId", regionId));
a(("applicationVersion", applicationVersion));
a(("websiteSiteName", websiteSiteName));
a(("id", options.Id));
}));
}
if (options.ApplicationInsightsKey != null)
{
var telemetryClient = new TelemetryClient(new TelemetryConfiguration(options.ApplicationInsightsKey))
{
InstrumentationKey = options.ApplicationInsightsKey
};
disposables.Add(telemetryClient.SubscribeToPocketLogger(_assembliesEmittingPocketLoggerLogs));
}
Log.Event("AgentStarting");
return disposables;
}
internal static IDisposable StartToolLogging(StartupOptions options)
{
var disposables = new CompositeDisposable();
if (options.LogPath != null)
{
var log = new SerilogLoggerConfiguration()
.WriteTo
.RollingFileAlternate(options.LogPath.FullName, outputTemplate: "{Message}{NewLine}")
.CreateLogger();
var subscription = LogEvents.Subscribe(
e => log.Information(e.ToLogString()),
_assembliesEmittingPocketLoggerLogs);
disposables.Add(subscription);
disposables.Add(log);
}
if (options.Verbose)
{
disposables.Add(
LogEvents.Subscribe(e => Console.WriteLine(e.ToLogString()),
_assembliesEmittingPocketLoggerLogs));
}
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
Log.Warning($"{nameof(TaskScheduler.UnobservedTaskException)}", args.Exception);
args.SetObserved();
};
return disposables;
}
public static IWebHost ConstructWebHost(StartupOptions options)
{
var disposables = new CompositeDisposable
{
StartAppInsightsLogging(options),
StartToolLogging(options)
};
if (options.Key is null)
{
Log.Trace("No Key Provided");
}
else
{
Log.Trace("Received Key: {key}", options.Key);
}
var webHost = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Path.GetDirectoryName(typeof(Program).Assembly.Location))
.ConfigureServices(c =>
{
if (!string.IsNullOrEmpty(options.ApplicationInsightsKey))
{
c.AddApplicationInsightsTelemetry(options.ApplicationInsightsKey);
}
c.AddSingleton(options);
foreach (var serviceDescriptor in _serviceCollection)
{
c.Add(serviceDescriptor);
}
})
.UseEnvironment(options.EnvironmentName)
.UseStartup<Startup>()
.ConfigureUrl(options.Mode, options.Port)
.Build();
return webHost;
}
}
}