Split out loggingStart service to its own file and namespace
This commit is contained in:
30
Installers/LoggingStartService.cs
Normal file
30
Installers/LoggingStartService.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
using ILogger = Serilog.ILogger;
|
||||||
|
|
||||||
|
namespace Diary.Installers
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
46
Program.cs
46
Program.cs
@@ -1,25 +1,22 @@
|
|||||||
using api.Launch;
|
|
||||||
using Diary.Data;
|
using Diary.Data;
|
||||||
using Diary.Installers;
|
using Diary.Installers;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using ILogger = Serilog.ILogger;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Settings - Done by sefaulr
|
* Settings - Done by sefaulr
|
||||||
* Logging - Done
|
* Logging - Done
|
||||||
* Database - Donw
|
* Database - Donw
|
||||||
* Dependencies
|
* Dependencies
|
||||||
* automapper
|
* automapper - Done
|
||||||
* Cors
|
* Cors
|
||||||
* Views/Filters/Validation
|
* Views/Filters/Validation
|
||||||
* Swagger
|
* Swagger - Done
|
||||||
* Auth
|
* Auth
|
||||||
*
|
*
|
||||||
* */
|
* */
|
||||||
|
|
||||||
|
|
||||||
//+Setup Logger
|
//+Setup Logger
|
||||||
Log.Logger = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
.MinimumLevel.Information()
|
.MinimumLevel.Information()
|
||||||
@@ -28,6 +25,7 @@ Log.Logger = new LoggerConfiguration()
|
|||||||
.WriteTo.Console()
|
.WriteTo.Console()
|
||||||
.WriteTo.Seq("http://seq.lan:5341", apiKey: "Jtfj82GQmcKTAh1kW3zI")
|
.WriteTo.Seq("http://seq.lan:5341", apiKey: "Jtfj82GQmcKTAh1kW3zI")
|
||||||
.WriteTo.File("Logs/Log.txt")
|
.WriteTo.File("Logs/Log.txt")
|
||||||
|
.Enrich.FromLogContext()
|
||||||
.CreateLogger();
|
.CreateLogger();
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -44,11 +42,13 @@ try
|
|||||||
//---Y
|
//---Y
|
||||||
//Database
|
//Database
|
||||||
builder.Services.AddDatabase(builder.Configuration);
|
builder.Services.AddDatabase(builder.Configuration);
|
||||||
|
builder.Services.AddScoped<IDiaryDBContext, DiaryDBContext>();
|
||||||
|
|
||||||
|
//---Y
|
||||||
|
//Automapper
|
||||||
|
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddScoped<IDiaryDBContext, DiaryDBContext>();
|
|
||||||
|
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
@@ -80,9 +80,6 @@ try
|
|||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -92,33 +89,4 @@ finally
|
|||||||
{
|
{
|
||||||
Log.Information("Shut down complete");
|
Log.Information("Shut down complete");
|
||||||
Log.CloseAndFlush();
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user