Logging sorted

This commit is contained in:
2022-06-10 23:18:02 +01:00
parent e7c1fde35e
commit d15571a98d
9 changed files with 139 additions and 38 deletions

View File

@@ -15,7 +15,7 @@ namespace Diary.Component.Entry.Repository
public int EnrtyID { get; set; }
[Required]
[Column(TypeName="Date")]
[Column(TypeName="date")]
public DateTime Date { get; set; }
[Required]

View File

@@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Diary.Data.Migrations
{
[DbContext(typeof(DiaryDBContext))]
[Migration("20220603063002_Initial")]
[Migration("20220603115417_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -33,7 +33,7 @@ namespace Diary.Data.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("EnrtyID"), 1L, 1);
b.Property<DateTime>("Date")
.HasColumnType("Date");
.HasColumnType("date");
b.Property<string>("Note")
.IsRequired()

View File

@@ -19,7 +19,7 @@ namespace Diary.Data.Migrations
{
EnrtyID = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Date = table.Column<DateTime>(type: "Date", nullable: false),
Date = table.Column<DateTime>(type: "date", nullable: false),
ValidFrom = table.Column<DateTime>(type: "datetime2", nullable: false),
ValidTo = table.Column<DateTime>(type: "datetime2", nullable: true),
Note = table.Column<string>(type: "nvarchar(max)", nullable: false)

View File

@@ -31,7 +31,7 @@ namespace Diary.Data.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("EnrtyID"), 1L, 1);
b.Property<DateTime>("Date")
.HasColumnType("Date");
.HasColumnType("date");
b.Property<string>("Note")
.IsRequired()

View File

@@ -15,7 +15,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diary.Installers
{
public static class DependencyInstallers
{
public static IServiceCollection AddDependencies(this IServiceCollection services)
{
return services;
}
}
}

1
Log.txt Normal file
View File

@@ -0,0 +1 @@
2022-06-04 14:39:36.631 +01:00 [INF] start

View File

@@ -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();
// Add services to the container.
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();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
try
{
app.UseSwagger();
app.UseSwaggerUI(opt =>
{
opt.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
opt.RoutePrefix = String.Empty;
}
);
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();
}
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;
}
}
}

View File

@@ -7,8 +7,9 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Default": "Trace",
"System": "Information",
"Microsoft": "None"
}
},
"AllowedHosts": "*"