Files
Diary/Program.cs
2022-06-10 23:18:02 +01:00

124 lines
2.6 KiB
C#

using api.Launch;
using Diary.Data;
using Diary.Installers;
using Serilog;
using Serilog.Events;
using System.Reflection;
using ILogger = Serilog.ILogger;
/*
* Settings - Done by sefaulr
* Logging - Done
* Database - Donw
* Dependencies
* automapper
* Cors
* Views/Filters/Validation
* Swagger
* Auth
*
* */
//+Setup Logger
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.WriteTo.Console()
.WriteTo.Seq("http://seq.lan:5341", apiKey: "Jtfj82GQmcKTAh1kW3zI")
.WriteTo.File("Logs/Log.txt")
.CreateLogger();
try
{
Log.Information("Starting up");
var builder = WebApplication.CreateBuilder(args);
//---Y
//Add Serilog
builder.Host.UseSerilog(Log.Logger);
builder.Services.AddSingleton<IHostedService>(new LoggingStartService(Log.Logger, Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().ManifestModule.ScopeName)));
//---Y
//Database
builder.Services.AddDatabase(builder.Configuration);
builder.Services.AddControllers();
builder.Services.AddScoped<IDiaryDBContext, DiaryDBContext>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//---R
//+Build
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(opt =>
{
opt.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
opt.RoutePrefix = String.Empty;
}
);
}
app.UseSerilogRequestLogging();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
namespace api.Launch
{
public class LoggingStartService : IHostedService
{
private readonly ILogger _log;
private readonly string _serviceName;
public ILogger Logger { get; }
public LoggingStartService(ILogger logger, string serviceName)
{
_log = logger.ForContext<LoggingStartService>();
_serviceName = serviceName ?? "API";
}
public Task StartAsync(CancellationToken cancellationToken)
{
_log.Information("{ServiceName} service started", _serviceName);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_log.Information("{ServiceName} service stopped", _serviceName);
return Task.CompletedTask;
}
}
}