Added logging nuget packages to allow for logging capabilities.

This commit is contained in:
Daniel Wathen
2022-12-09 14:17:39 -07:00
parent 32d8ad8b3c
commit 43957fe66d
7 changed files with 91 additions and 8 deletions

View File

@ -23,6 +23,10 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -15,6 +15,7 @@ namespace ReportingServices.Controllers
public IActionResult Index() public IActionResult Index()
{ {
_logger.LogInformation("Starting Index Page");
return View(); return View();
} }

View File

@ -12,16 +12,19 @@ namespace ReportingServices.Controllers
private readonly IJsonFileHandler _jsonFileHandler; private readonly IJsonFileHandler _jsonFileHandler;
private readonly IScrapeDatabaseRepository _scrapeDatabaseRepository; private readonly IScrapeDatabaseRepository _scrapeDatabaseRepository;
private readonly IFabTimeReportingRepository _fabTimeReportingRepository; private readonly IFabTimeReportingRepository _fabTimeReportingRepository;
private readonly ILogger<ProductionReportController> _logger;
private readonly int _reportIndex = (int)DateTime.Now.DayOfWeek; private readonly int _reportIndex = (int)DateTime.Now.DayOfWeek;
private readonly string _dailyRptFilePath = "wwwroot/Assets/DailyReportInfo.json"; private readonly string _dailyRptFilePath = "wwwroot/Assets/DailyReportInfo.json";
private readonly string _toolStateOwnerFilePath = "wwwroot/Assets/ToolStates.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"; 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<ProductionReportController> logger)
{ {
_jsonFileHandler = jsonFileHandler; _jsonFileHandler = jsonFileHandler;
_scrapeDatabaseRepository = scrapeDatabaseRepository; _scrapeDatabaseRepository = scrapeDatabaseRepository;
_fabTimeReportingRepository = fabTimeReportingRepository; _fabTimeReportingRepository = fabTimeReportingRepository;
_logger = logger;
} }
@ -32,12 +35,21 @@ namespace ReportingServices.Controllers
public IActionResult DailyReport() public IActionResult DailyReport()
{ {
DailyReport dailyReport = SetUpDailyReport(); try
Dictionary<string, List<string>> toolStateOwners = _jsonFileHandler.LoadJSONFile<Dictionary<string, List<string>>>(_toolStateOwnerFilePath); {
DailyReport dailyReport = SetUpDailyReport();
Dictionary<string, List<string>> toolStateOwners = _jsonFileHandler.LoadJSONFile<Dictionary<string, List<string>>>(_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() public IActionResult EditDailyReport()

View File

@ -1,6 +1,16 @@
using Microsoft.AspNetCore.Builder;
using ReportingServices.Dependency_Injections; using ReportingServices.Dependency_Injections;
using Serilog;
LoggerConfiguration loggerConfiguration = new();
var builder = WebApplication.CreateBuilder(args); 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<Program>();
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
@ -28,5 +38,17 @@ app.UseAuthorization();
app.MapControllerRoute( app.MapControllerRoute(
name: "default", name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"); pattern: "{controller=Home}/{action=Index}/{id?}");
_ = app.Lifetime.ApplicationStopped.Register(Log.CloseAndFlush);
app.Run(); log.Information("Starting Web Application");
try
{
app.Run();
}
catch (Exception ex)
{
log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}

View File

@ -7,6 +7,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -4,5 +4,8 @@
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
},
"Serilog": {
"MinimumLevel": "Debug"
} }
} }

View File

@ -5,5 +5,42 @@
"Microsoft.AspNetCore": "Warning" "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"
}
}
} }