-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathProgram.cs
167 lines (127 loc) · 6.26 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
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
162
163
164
165
166
167
using Azure.Storage.Blobs;
using LearnSmartCoding.CosmosDb.Linq.API.Data;
using LearnSmartCoding.CosmosDb.Linq.API.Model;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.Azure.Cosmos;
using Serilog;
using Serilog.Events;
using System.Collections;
using System.Net;
namespace LearnSmartCoding.CosmosDb.Linq.API
{
public class Program
{
public static void Main(string[] args)
{
try
{
var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration;
builder.Services.AddApplicationInsightsTelemetry();
// for local storage
//Log.Logger = new LoggerConfiguration()
// .WriteTo.Console()
// .MinimumLevel.Information()
// .WriteTo.File("/app/logs/application.log", rollingInterval: RollingInterval.Day,
// fileSizeLimitBytes: 5242880, retainedFileCountLimit: 7,
// outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
// .Enrich.FromLogContext()
// .CreateBootstrapLogger();
// Read the Azure Blob Storage settings from appsettings.json
var azureBlobStorageSettings = configuration.GetSection("AzureBlobStorage").Get<AzureBlobStorageSettings>();
// Configure Serilog with the settings
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Information()
.WriteTo.AzureBlobStorage(
blobServiceClient: new BlobServiceClient(azureBlobStorageSettings?.ConnectionString),
restrictedToMinimumLevel: LogEventLevel.Verbose,
storageContainerName: azureBlobStorageSettings?.ContainerName,
storageFileName: "application.log",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
retainedBlobCountLimit: 7,
blobSizeLimitBytes: 5242880,
useUtcTimeZone: true
)
.Enrich.FromLogContext()
.CreateLogger();
builder.Host.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
.WriteTo.ApplicationInsights(
services.GetRequiredService<TelemetryConfiguration>(),
TelemetryConverter.Traces));
Log.Information("Starting the application...");
// Add CosmosClient and configure logging
builder.Services.AddSingleton((provider) =>
{
var endpointUri = configuration["CosmosDbSettings:EndpointUri"];
var primaryKey = configuration["CosmosDbSettings:PrimaryKey"];
var databaseName = configuration["CosmosDbSettings:DatabaseName"];
var cosmosClientOptions = new CosmosClientOptions
{
ApplicationName = databaseName,
ConnectionMode = ConnectionMode.Gateway,
//ServerCertificateCustomValidationCallback = (request, certificate, chain) =>
//{
// // Always return true to ignore certificate validation errors
// return true; //not for production
//}
};
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});
var cosmosClient = new CosmosClient(endpointUri, primaryKey, cosmosClientOptions);
return cosmosClient;
});
builder.Services.AddControllers();
// In production, modify this with the actual domains you want to allow
builder.Services.AddCors(o => o.AddPolicy("default", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<ITaskRepository, TaskRepository>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
var app = builder.Build();
// Enable Serilog exception logging
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = exceptionHandlerPathFeature?.Error;
Log.Error(exception, "Unhandled exception occurred. {ExceptionDetails}", exception?.ToString());
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
await context.Response.WriteAsync("An unexpected error occurred. Please try again later.");
});
});
app.UseMiddleware<RequestResponseLoggingMiddleware>();
// Configure the HTTP request pipeline.
// if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("default");
app.UseAuthorization();
app.MapControllers();
app.Run();
Log.Information("Application started successfully.");
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
}
}