-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (63 loc) · 2.88 KB
/
Program.cs
File metadata and controls
78 lines (63 loc) · 2.88 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
using AutoMapper;
using CollegeProject.Configurations;
using CollegeProject.Data;
using CollegeProject.Data.Repository;
using CollegeProject.MyLogging;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
/*
builder.Logging.ClearProviders(); //Removes all types of logging mechanisms from application
builder.Logging.AddConsole(); //Logs only to console
builder.Logging.AddDebug();//Logs only to debug
*/
#region Serilog Configuration
/*
//Using SeriLog
//logs to file - with minimum level as Information. Log will be generated with time gap as specified.(Minutes, days, Hours, etc.,)
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.File("Log/log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
//builder.Host.UseSerilog(); //Only to use Serilog by overriding existing loggers.
builder.Logging.AddSerilog(); // To add Serilog logging with other type of existing loggings as well.
*/
#endregion
//Clearing existing logging mechanisms
builder.Logging.ClearProviders();
builder.Logging.AddLog4Net();
//configuring sql server connection string feom appsettings.json
builder.Services.AddDbContext<CollegeDBContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("CollegeAppDBConnection"));
});
// Add services to the container.
//Content Negotiation -> How to now allow other response types like xml, bla bla...
//Shows 406 Status Code -> Which means not acceptable data format when trying to return other types apart from JSON.
//builder.Services.AddControllers(options => options.ReturnHttpNotAcceptable = true).AddNewtonsoftJson();
//Below Line is added to support XML data............
builder.Services.AddControllers(options => options.ReturnHttpNotAcceptable = true).AddNewtonsoftJson().AddXmlDataContractSerializerFormatters();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//builder.Services.AddAutoMapper(typeof(AutomapperConfig));
builder.Services.AddAutoMapper(typeof(AutomapperConfig));
//builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
//For loosely coupled Dependency Injection - You will just have to make your changes at one place...
//Easier to maintain
builder.Services.AddScoped<IMyLoggerr, LogToFile>();
builder.Services.AddScoped<IStudentRepository, StudentRepository>();
builder.Services.AddScoped(typeof(ICollegeRepository<>), typeof(CollegeRepository<>)); //Common generic Repository which you can use to inject any repository,
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//testing commits
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();