diff --git a/ReportingServices.Test/ReportingServices.Test.csproj b/ReportingServices.Test/ReportingServices.Test.csproj index 027f1e2..4621b7d 100644 --- a/ReportingServices.Test/ReportingServices.Test.csproj +++ b/ReportingServices.Test/ReportingServices.Test.csproj @@ -23,6 +23,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/ReportingServices/Controllers/HomeController.cs b/ReportingServices/Controllers/HomeController.cs index 96bfcc5..3c490e2 100644 --- a/ReportingServices/Controllers/HomeController.cs +++ b/ReportingServices/Controllers/HomeController.cs @@ -15,6 +15,7 @@ namespace ReportingServices.Controllers public IActionResult Index() { + _logger.LogInformation("Starting Index Page"); return View(); } diff --git a/ReportingServices/Controllers/ProductionReportController.cs b/ReportingServices/Controllers/ProductionReportController.cs index 38ce5a2..4451676 100644 --- a/ReportingServices/Controllers/ProductionReportController.cs +++ b/ReportingServices/Controllers/ProductionReportController.cs @@ -12,16 +12,19 @@ namespace ReportingServices.Controllers private readonly IJsonFileHandler _jsonFileHandler; private readonly IScrapeDatabaseRepository _scrapeDatabaseRepository; private readonly IFabTimeReportingRepository _fabTimeReportingRepository; + private readonly ILogger _logger; private readonly int _reportIndex = (int)DateTime.Now.DayOfWeek; private readonly string _dailyRptFilePath = "wwwroot/Assets/DailyReportInfo.json"; private readonly string _toolStateOwnerFilePath = "wwwroot/Assets/ToolStates.json"; private readonly string _toolFilter = "~R76%2C%20~R78%2C%20~R25%2C%20~R67%2C%20~R69%2C%20~R71%2C%20~R47%2C%20~R51%2C%20~R28"; - public ProductionReportController(IJsonFileHandler jsonFileHandler, IScrapeDatabaseRepository scrapeDatabaseRepository, IFabTimeReportingRepository fabTimeReportingRepository) + public ProductionReportController(IJsonFileHandler jsonFileHandler, IScrapeDatabaseRepository scrapeDatabaseRepository, + IFabTimeReportingRepository fabTimeReportingRepository, ILogger logger) { _jsonFileHandler = jsonFileHandler; _scrapeDatabaseRepository = scrapeDatabaseRepository; _fabTimeReportingRepository = fabTimeReportingRepository; + _logger = logger; } @@ -32,12 +35,21 @@ namespace ReportingServices.Controllers public IActionResult DailyReport() { - DailyReport dailyReport = SetUpDailyReport(); - Dictionary> toolStateOwners = _jsonFileHandler.LoadJSONFile>>(_toolStateOwnerFilePath); + try + { + DailyReport dailyReport = SetUpDailyReport(); + Dictionary> toolStateOwners = _jsonFileHandler.LoadJSONFile>>(_toolStateOwnerFilePath); - dailyReport.ToolStatesByOwner = toolStateOwners; + dailyReport.ToolStatesByOwner = toolStateOwners; - return View(dailyReport); + return View(dailyReport); + } + catch (Exception ex) + { + _logger.LogCritical(ex, "Failed to load report"); + return View(); + } + } public IActionResult EditDailyReport() diff --git a/ReportingServices/Program.cs b/ReportingServices/Program.cs index e6a5305..c4cbf25 100644 --- a/ReportingServices/Program.cs +++ b/ReportingServices/Program.cs @@ -1,6 +1,16 @@ +using Microsoft.AspNetCore.Builder; using ReportingServices.Dependency_Injections; +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.AddControllersWithViews(); @@ -28,5 +38,17 @@ app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); - -app.Run(); +_ = app.Lifetime.ApplicationStopped.Register(Log.CloseAndFlush); +log.Information("Starting Web Application"); +try +{ + app.Run(); +} +catch (Exception ex) +{ + log.Fatal(ex, "Host terminated unexpectedly"); +} +finally +{ + Log.CloseAndFlush(); +} diff --git a/ReportingServices/ReportingServices.csproj b/ReportingServices/ReportingServices.csproj index d6c00d7..af6537a 100644 --- a/ReportingServices/ReportingServices.csproj +++ b/ReportingServices/ReportingServices.csproj @@ -7,6 +7,10 @@ + + + + diff --git a/ReportingServices/appsettings.Development.json b/ReportingServices/appsettings.Development.json index 0c208ae..2f4b910 100644 --- a/ReportingServices/appsettings.Development.json +++ b/ReportingServices/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "Serilog": { + "MinimumLevel": "Debug" } } diff --git a/ReportingServices/appsettings.json b/ReportingServices/appsettings.json index 10f68b8..7e883f3 100644 --- a/ReportingServices/appsettings.json +++ b/ReportingServices/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" + } + } }