Added logging to application.

This commit is contained in:
Daniel Wathen 2023-01-06 09:15:40 -07:00
parent 6de41bc8da
commit 9ee0b14ee9
6 changed files with 94 additions and 12 deletions

View File

@ -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<Program>();
// 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();
}

View File

@ -11,6 +11,10 @@
<VSTestResultsDirectory>../.vscode/TestResults</VSTestResultsDirectory>
</PropertyGroup>
<ItemGroup>
<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" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@ -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"
}
}
}

View File

@ -12,6 +12,10 @@
</PropertyGroup>
<ItemGroup>
<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>
</Project>

View File

@ -6,13 +6,17 @@ namespace ReportingServices.UI.Controllers
{
public class PlanningReportController : Controller
{
private readonly ILogger<PlanningReportController> _logger;
private readonly IConfiguration _configuration;
private readonly string _baseUrl;
public PlanningReportController(IConfiguration configuration)
public PlanningReportController(ILogger<PlanningReportController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
_baseUrl = _configuration.GetValue<string>("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();
_logger.LogInformation("Part Change URL: {url}", partChangeUrl);
_logger.LogInformation("PSN WO Runs URL: {url}", psnwoRunsUrl);
WeeklyPartChanges weeklyPartChanges = new();
try
{
int numberOfPartChanges = await ApiCaller.GetApi<int>(partChangeUrl);
List<ReactorPSNWORuns> reactorPSNWORuns = await ApiCaller.GetApi<List<ReactorPSNWORuns>>(psnwoRunsUrl);
WeeklyPartChanges weeklyPartChanges = new()
weeklyPartChanges.TotalPartChanges = numberOfPartChanges;
weeklyPartChanges.StartDate = startDate.ToShortDateString();
weeklyPartChanges.EndDate = endDate.ToShortDateString();
weeklyPartChanges.ReactorPSNWORuns = reactorPSNWORuns;
}
catch (Exception ex)
{
TotalPartChanges = numberOfPartChanges,
StartDate = startDate.ToShortDateString(),
EndDate = endDate.ToShortDateString(),
ReactorPSNWORuns = reactorPSNWORuns
};
_logger.LogCritical(ex, "Failed to get a response from API calls.");
RedirectToAction("Error");
}
return View(weeklyPartChanges);
}

View File

@ -18,6 +18,8 @@ namespace ReportingServices.UI.Controllers
_logger = logger;
_configuration = configuration;
_baseUrl = _configuration.GetValue<string>("BaseAPIAddress");
_logger.LogInformation("Base API Address: {baseUrl}", _baseUrl);
}