diff --git a/ReportingServices.API/Program.cs b/ReportingServices.API/Program.cs index 8f9d30e..1322b3f 100644 --- a/ReportingServices.API/Program.cs +++ b/ReportingServices.API/Program.cs @@ -1,7 +1,14 @@ using ReportingServices.Shared.Repositories; +using Serilog; + +LoggerConfiguration loggerConfiguration = new(); var builder = WebApplication.CreateBuilder(args); - +Environment.SetEnvironmentVariable("workingDirectory", "C:/tmp/logging"); +_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, builder.Configuration); +_ = SerilogHostBuilderExtensions.UseSerilog(builder.Host); +Log.Logger = loggerConfiguration.CreateLogger(); +Serilog.ILogger log = Log.ForContext(); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle @@ -24,5 +31,18 @@ app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); +_ = app.Lifetime.ApplicationStopped.Register(Log.CloseAndFlush); +log.Information("Starting Web APIs"); -app.Run(); +try +{ + app.Run(); +} +catch (Exception ex) +{ + log.Fatal(ex, "Host terminated unexpectedly"); +} +finally +{ + Log.CloseAndFlush(); +} diff --git a/ReportingServices.API/ReportingServices.API.csproj b/ReportingServices.API/ReportingServices.API.csproj index c257633..2a98c37 100644 --- a/ReportingServices.API/ReportingServices.API.csproj +++ b/ReportingServices.API/ReportingServices.API.csproj @@ -11,6 +11,10 @@ ../.vscode/TestResults + + + + diff --git a/ReportingServices.API/appsettings.json b/ReportingServices.API/appsettings.json index 10f68b8..7e883f3 100644 --- a/ReportingServices.API/appsettings.json +++ b/ReportingServices.API/appsettings.json @@ -5,5 +5,42 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Serilog": { + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.File" + ], + "MinimumLevel": "Information", + "WriteTo": [ + { + "Name": "Debug", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "Console", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": { + "path": "%workingDirectory% - Log/log-.txt", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}", + "rollingInterval": "Hour" + } + } + ], + "Enrich": [ + "FromLogContext", + "WithMachineName", + "WithThreadId" + ], + "Properties": { + "Application": "Sample" + } + } } diff --git a/ReportingServices.Shared/ReportingServices.Shared.csproj b/ReportingServices.Shared/ReportingServices.Shared.csproj index d74e6fc..2cd4803 100644 --- a/ReportingServices.Shared/ReportingServices.Shared.csproj +++ b/ReportingServices.Shared/ReportingServices.Shared.csproj @@ -12,6 +12,10 @@ + + + + diff --git a/ReportingServices.UI/Controllers/PlanningReportController.cs b/ReportingServices.UI/Controllers/PlanningReportController.cs index 8bb02cf..0e684b8 100644 --- a/ReportingServices.UI/Controllers/PlanningReportController.cs +++ b/ReportingServices.UI/Controllers/PlanningReportController.cs @@ -6,13 +6,17 @@ namespace ReportingServices.UI.Controllers { public class PlanningReportController : Controller { + private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly string _baseUrl; - public PlanningReportController(IConfiguration configuration) + public PlanningReportController(ILogger logger, IConfiguration configuration) { + _logger = logger; _configuration = configuration; _baseUrl = _configuration.GetValue("BaseAPIAddress") + "ScrapeDB/"; + + _logger.LogInformation("Base API Address: {baseUrl}", _baseUrl); } public IActionResult Index() @@ -25,16 +29,27 @@ namespace ReportingServices.UI.Controllers string partChangeUrl = _baseUrl + "PartChanges?startDate=" + startDate.ToString() + "&endDate=" + endDate.ToString(); string psnwoRunsUrl = _baseUrl + "PSNWO?startDate=" + startDate.ToString() + "&endDate=" + endDate.ToString(); - int numberOfPartChanges = await ApiCaller.GetApi(partChangeUrl); - List reactorPSNWORuns = await ApiCaller.GetApi>(psnwoRunsUrl); + _logger.LogInformation("Part Change URL: {url}", partChangeUrl); + _logger.LogInformation("PSN WO Runs URL: {url}", psnwoRunsUrl); - WeeklyPartChanges weeklyPartChanges = new() + WeeklyPartChanges weeklyPartChanges = new(); + + try { - TotalPartChanges = numberOfPartChanges, - StartDate = startDate.ToShortDateString(), - EndDate = endDate.ToShortDateString(), - ReactorPSNWORuns = reactorPSNWORuns - }; + int numberOfPartChanges = await ApiCaller.GetApi(partChangeUrl); + List reactorPSNWORuns = await ApiCaller.GetApi>(psnwoRunsUrl); + + weeklyPartChanges.TotalPartChanges = numberOfPartChanges; + weeklyPartChanges.StartDate = startDate.ToShortDateString(); + weeklyPartChanges.EndDate = endDate.ToShortDateString(); + weeklyPartChanges.ReactorPSNWORuns = reactorPSNWORuns; + } + catch (Exception ex) + { + _logger.LogCritical(ex, "Failed to get a response from API calls."); + RedirectToAction("Error"); + } + return View(weeklyPartChanges); } diff --git a/ReportingServices.UI/Controllers/ProductionReportController.cs b/ReportingServices.UI/Controllers/ProductionReportController.cs index 51addc4..11c4e53 100644 --- a/ReportingServices.UI/Controllers/ProductionReportController.cs +++ b/ReportingServices.UI/Controllers/ProductionReportController.cs @@ -18,6 +18,8 @@ namespace ReportingServices.UI.Controllers _logger = logger; _configuration = configuration; _baseUrl = _configuration.GetValue("BaseAPIAddress"); + + _logger.LogInformation("Base API Address: {baseUrl}", _baseUrl); }