Logging sorted
This commit is contained in:
137
Program.cs
137
Program.cs
@@ -1,47 +1,124 @@
|
||||
using api.Launch;
|
||||
using Diary.Data;
|
||||
using Diary.Installers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using System.Reflection;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
//GetConfigStuff
|
||||
IConfigurationRoot config = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json",false)
|
||||
.Build();
|
||||
/*
|
||||
* Settings - Done by sefaulr
|
||||
* Logging - Done
|
||||
* Database - Donw
|
||||
* Dependencies
|
||||
* automapper
|
||||
* Cors
|
||||
* Views/Filters/Validation
|
||||
* Swagger
|
||||
* Auth
|
||||
*
|
||||
* */
|
||||
|
||||
|
||||
builder.Services.AddDatabase(config);
|
||||
//+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);
|
||||
|
||||
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddScoped<IDiaryDBContext, DiaryDBContext>();
|
||||
|
||||
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();
|
||||
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(opt =>
|
||||
{
|
||||
opt.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
|
||||
opt.RoutePrefix = String.Empty;
|
||||
}
|
||||
);
|
||||
Log.Fatal(ex, "Unhandled exception");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.Information("Shut down complete");
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
namespace api.Launch
|
||||
{
|
||||
public class LoggingStartService : IHostedService
|
||||
{
|
||||
private readonly ILogger _log;
|
||||
private readonly string _serviceName;
|
||||
|
||||
app.UseAuthorization();
|
||||
public ILogger Logger { get; }
|
||||
|
||||
app.MapControllers();
|
||||
public LoggingStartService(ILogger logger, string serviceName)
|
||||
{
|
||||
_log = logger.ForContext<LoggingStartService>();
|
||||
_serviceName = serviceName ?? "API";
|
||||
}
|
||||
|
||||
app.Run();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user