From d15571a98d2e13deb4250dcd00e4672b9c61b81e Mon Sep 17 00:00:00 2001 From: Tim Cliff Date: Fri, 10 Jun 2022 23:18:02 +0100 Subject: [PATCH] Logging sorted --- Component/Entry/Repository/Entry.cs | 2 +- ....cs => 20220603115417_Initial.Designer.cs} | 4 +- ...2_Initial.cs => 20220603115417_Initial.cs} | 2 +- .../Migrations/DiaryDBContextModelSnapshot.cs | 2 +- Diary.csproj | 6 +- Installers/DependencyInstallers.cs | 18 +++ Log.txt | 1 + Program.cs | 137 ++++++++++++++---- appsettings.json | 5 +- 9 files changed, 139 insertions(+), 38 deletions(-) rename Data/Migrations/{20220603063002_Initial.Designer.cs => 20220603115417_Initial.Designer.cs} (94%) rename Data/Migrations/{20220603063002_Initial.cs => 20220603115417_Initial.cs} (95%) create mode 100644 Installers/DependencyInstallers.cs create mode 100644 Log.txt diff --git a/Component/Entry/Repository/Entry.cs b/Component/Entry/Repository/Entry.cs index dd2a7de..bca7047 100644 --- a/Component/Entry/Repository/Entry.cs +++ b/Component/Entry/Repository/Entry.cs @@ -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] diff --git a/Data/Migrations/20220603063002_Initial.Designer.cs b/Data/Migrations/20220603115417_Initial.Designer.cs similarity index 94% rename from Data/Migrations/20220603063002_Initial.Designer.cs rename to Data/Migrations/20220603115417_Initial.Designer.cs index ac28481..e2a4334 100644 --- a/Data/Migrations/20220603063002_Initial.Designer.cs +++ b/Data/Migrations/20220603115417_Initial.Designer.cs @@ -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("EnrtyID"), 1L, 1); b.Property("Date") - .HasColumnType("Date"); + .HasColumnType("date"); b.Property("Note") .IsRequired() diff --git a/Data/Migrations/20220603063002_Initial.cs b/Data/Migrations/20220603115417_Initial.cs similarity index 95% rename from Data/Migrations/20220603063002_Initial.cs rename to Data/Migrations/20220603115417_Initial.cs index d62f13e..7d53c0e 100644 --- a/Data/Migrations/20220603063002_Initial.cs +++ b/Data/Migrations/20220603115417_Initial.cs @@ -19,7 +19,7 @@ namespace Diary.Data.Migrations { EnrtyID = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), - Date = table.Column(type: "Date", nullable: false), + Date = table.Column(type: "date", nullable: false), ValidFrom = table.Column(type: "datetime2", nullable: false), ValidTo = table.Column(type: "datetime2", nullable: true), Note = table.Column(type: "nvarchar(max)", nullable: false) diff --git a/Data/Migrations/DiaryDBContextModelSnapshot.cs b/Data/Migrations/DiaryDBContextModelSnapshot.cs index 28b256e..05ad204 100644 --- a/Data/Migrations/DiaryDBContextModelSnapshot.cs +++ b/Data/Migrations/DiaryDBContextModelSnapshot.cs @@ -31,7 +31,7 @@ namespace Diary.Data.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("EnrtyID"), 1L, 1); b.Property("Date") - .HasColumnType("Date"); + .HasColumnType("date"); b.Property("Note") .IsRequired() diff --git a/Diary.csproj b/Diary.csproj index 91ff7c5..0ad0a69 100644 --- a/Diary.csproj +++ b/Diary.csproj @@ -15,7 +15,11 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + + + + + diff --git a/Installers/DependencyInstallers.cs b/Installers/DependencyInstallers.cs new file mode 100644 index 0000000..57bcb12 --- /dev/null +++ b/Installers/DependencyInstallers.cs @@ -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; + + } + + } +} diff --git a/Log.txt b/Log.txt new file mode 100644 index 0000000..89a0575 --- /dev/null +++ b/Log.txt @@ -0,0 +1 @@ +2022-06-04 14:39:36.631 +01:00 [INF] start diff --git a/Program.cs b/Program.cs index aad0036..09815f5 100644 --- a/Program.cs +++ b/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(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(); -builder.Services.AddControllers(); -builder.Services.AddScoped(); + // 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(); + _serviceName = serviceName ?? "API"; + } -app.Run(); \ No newline at end of file + 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; + } + } +} \ No newline at end of file diff --git a/appsettings.json b/appsettings.json index b97fc1f..95f123b 100644 --- a/appsettings.json +++ b/appsettings.json @@ -7,8 +7,9 @@ "Logging": { "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Default": "Trace", + "System": "Information", + "Microsoft": "None" } }, "AllowedHosts": "*"