diff --git a/src/UL.UI.Web/Pages/Factorial.cshtml b/src/UL.UI.Web/Pages/Factorial.cshtml
new file mode 100644
index 0000000..af80c3c
--- /dev/null
+++ b/src/UL.UI.Web/Pages/Factorial.cshtml
@@ -0,0 +1,50 @@
+@page
+@model FactorialModel
+@{
+ ViewData["Title"] = "Factorial Test";
+}
+@ViewData["Title"]
+
+
+
+
+Results
+
+@if (!string.IsNullOrEmpty(Model.Result))
+{
+ The factorial of @Model.Value is @Model.Result
+}
+
+
+
+
+
+
+ @Model.ErrorMessage
+
+
+
+
+
+
+@if (!string.IsNullOrEmpty(Model.ErrorMessage))
+{
+
+}
\ No newline at end of file
diff --git a/src/UL.UI.Web/Pages/Factorial.cshtml.cs b/src/UL.UI.Web/Pages/Factorial.cshtml.cs
new file mode 100644
index 0000000..d3ad430
--- /dev/null
+++ b/src/UL.UI.Web/Pages/Factorial.cshtml.cs
@@ -0,0 +1,49 @@
+// --------------------------------------------------------------------------------------------------------------------
+//
+// Copyright 2023 (c) Bugail Consulting Ltd. All rights reserved.
+//
+// --------------------------------------------------------------------------------------------------------------------
+
+using Microsoft.AspNetCore.Mvc.RazorPages;
+
+namespace UL.UI.Web.Pages
+{
+ using System.ComponentModel.DataAnnotations;
+ using Microsoft.AspNetCore.Mvc;
+ using UL.Abstractions.Interfaces;
+
+ public class FactorialModel : PageModel
+ {
+ private readonly ILogger logger;
+ private readonly IFactorialService service;
+
+ public FactorialModel(IFactorialService service, ILoggerlogger)
+ {
+ this.service = service;
+ this.logger = logger;
+ this.Value = 3;
+ }
+
+ public void OnGet()
+ {
+ try
+ {
+ this.logger.LogInformation("OnGet - Running Factorial - Value: {Value}", this.Value);
+ this.Result = this.service.Calculate(this.Value).ToString();
+ }
+ catch (Exception e)
+ {
+ this.ErrorMessage = e.Message;
+ this.logger.LogError(e, "OnGet - Error - Value: {Value}, End: {End}", this.Value );
+ }
+ }
+
+ [FromQuery]
+ [Range(1,100)]
+ public int Value { get; set; }
+
+ public string Result { get; set; }
+
+ public string ErrorMessage { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/UL.UI.Web/Pages/FizzBuzz.cshtml.cs b/src/UL.UI.Web/Pages/FizzBuzz.cshtml.cs
index d418c06..ea68cb0 100644
--- a/src/UL.UI.Web/Pages/FizzBuzz.cshtml.cs
+++ b/src/UL.UI.Web/Pages/FizzBuzz.cshtml.cs
@@ -15,10 +15,12 @@ namespace UL.UI.Web.Pages
public class FizzBuzzModel : PageModel
{
private readonly IFizzBuzzService service;
+ private readonly ILogger logger;
- public FizzBuzzModel(IFizzBuzzService service)
+ public FizzBuzzModel(IFizzBuzzService service, ILoggerlogger)
{
this.service = service;
+ this.logger = logger;
this.Start = 1;
this.End = 100;
}
@@ -27,16 +29,18 @@ public void OnGet()
{
try
{
+ this.logger.LogInformation("OnGet - Running FizzBuzz - Start: {Start}, End: {End}", this.Start, this.End);
var collection = Enumerable.Range(this.Start, this.End);
this.Results = this.service.GetFizzBuzzList(collection);
}
catch (Exception e)
{
this.ErrorMessage = e.Message;
+ this.logger.LogError(e, "OnGet - Error - Start: {Start}, End: {End}", this.Start, this.End);
}
}
- public IList Results { get; set; }
+ public IEnumerable Results { get; set; }
[FromQuery]
[Range(1,100)]
diff --git a/src/UL.UI.Web/Pages/Shared/_Layout.cshtml b/src/UL.UI.Web/Pages/Shared/_Layout.cshtml
index 81f4d5c..ec8f795 100644
--- a/src/UL.UI.Web/Pages/Shared/_Layout.cshtml
+++ b/src/UL.UI.Web/Pages/Shared/_Layout.cshtml
@@ -25,6 +25,10 @@
FizBuzz
+
+ Factorial
+
+
diff --git a/src/UL.UI.Web/Program.cs b/src/UL.UI.Web/Program.cs
index 7f4a467..61acf5c 100644
--- a/src/UL.UI.Web/Program.cs
+++ b/src/UL.UI.Web/Program.cs
@@ -42,12 +42,7 @@ public static int Main(string[] args)
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.ReadFrom.Configuration(GetLoggerConfiguration())
- .WriteTo.Debug()
- .CreateLogger();
-
- // Create a Microsoft.Extensions.Logging.ILoggerFactory from the serilog static logger
- var loggerFactory = new SerilogLoggerFactory(Log.Logger);
- startUpLogger = loggerFactory.CreateLogger();
+ .CreateBootstrapLogger();
try
{
@@ -81,13 +76,9 @@ private static WebApplication CreateWebApplication(string[] args)
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
- builder.Host
- .UseSerilog((hostingContext, loggerConfiguration) =>
- {
- loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
- .Enrich.FromLogContext()
- .Enrich.WithProperty(nameof(hostingContext.HostingEnvironment.EnvironmentName), hostingContext.HostingEnvironment.EnvironmentName);
- })
+ builder
+ .Host
+ .UseSerilog()
.ConfigureServices(services => ConfigureServices(services, builder));
var app = BuildWebApplication(builder);
diff --git a/src/UL.UI.Web/loggingSettings.json b/src/UL.UI.Web/loggingSettings.json
index 64c6f56..03ce319 100644
--- a/src/UL.UI.Web/loggingSettings.json
+++ b/src/UL.UI.Web/loggingSettings.json
@@ -1,6 +1,6 @@
{
"Serilog": {
- "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Async", "Serilog.Sinks.ApplicationInsights" ],
+ "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Async" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
@@ -17,13 +17,6 @@
"Args": {
"outputTemplate": "\"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
}
- },
- {
- "Name": "ApplicationInsights",
- "Args": {
- "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights",
- "outputTemplate": "[{Component}|{MachineName}|{ThreadId}] {Timestamp:yyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] <{SourceContext}> {Message:lj}{NewLine}{Exception}"
- }
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
diff --git a/src/Ul.Services/FizzBuzzService.cs b/src/Ul.Services/FizzBuzzService.cs
index b174907..84e8a45 100644
--- a/src/Ul.Services/FizzBuzzService.cs
+++ b/src/Ul.Services/FizzBuzzService.cs
@@ -6,11 +6,16 @@
namespace UL.Services
{
+ using System;
using System.Collections.Generic;
+ using System.Linq;
using System.Text;
using Ardalis.GuardClauses;
+ using FluentValidation;
using Microsoft.Extensions.Logging;
using UL.Abstractions.Interfaces;
+ using UL.Core.Requests;
+ using UL.Core.Validators;
///
/// The fizzbuzz service.
@@ -18,22 +23,21 @@ namespace UL.Services
public class FizzBuzzService : IFizzBuzzService
{
private readonly IEnumerable strategies;
+ private readonly ILogger logger;
///
/// Initializes a new instance of the class.
///
/// A list of
- public FizzBuzzService(IEnumerable strategies)
+ /// The logger.
+ public FizzBuzzService(IEnumerable strategies, ILogger logger)
{
this.strategies = strategies;
+ this.logger = logger;
}
- ///
- /// Gets the fizzbuzz list of results.
- ///
- /// The collection of numbers to check.
- /// A List of .
- public IList GetFizzBuzzList(IEnumerable collection)
+ ///
+ public IEnumerable GetFizzBuzzList(IEnumerable collection)
{
Guard.Against.NullOrEmpty(collection, nameof(collection));
@@ -56,5 +60,22 @@ public IList GetFizzBuzzList(IEnumerable collection)
return list;
}
+
+ ///
+ public IEnumerable GetFizzBuzzList(FizzBuzzRequest request)
+ {
+ Guard.Against.Null(request, nameof(request));
+
+ var validator = new FizzBuzzRequestValidator();
+ var result = validator.Validate(request);
+
+ if (result.IsValid)
+ {
+ var list = Enumerable.Range(Convert.ToInt32(request.Start), Convert.ToInt32(request.End)).ToList();
+ return this.GetFizzBuzzList(list);
+ }
+
+ throw new ValidationException(result.ToString());
+ }
}
}
\ No newline at end of file