logger)
+ {
+ _next = next;
+ _logger = logger;
+ }
+
+ public async Task Invoke(HttpContext context)
+ {
+ try
+ {
+ await _next(context);
+ }
+ catch (Exception ex)
+ {
+ await HandleException(context, ex);
+ }
+ }
+
+ private Task HandleException(HttpContext context, Exception ex)
+ {
+ _logger.LogWarning(ex, "");
+
+ var code = ex switch
+ {
+ AuthenticationException _ => HttpStatusCode.Unauthorized,
+ EntityNotFoundException _ => HttpStatusCode.NotFound,
+ ArgumentException _ => HttpStatusCode.BadRequest,
+ DbUpdateConcurrencyException _ => HttpStatusCode.Conflict,
+ _ => HttpStatusCode.InternalServerError,
+ };
+
+ var result = JsonConvert.SerializeObject(new { error = ex.Message, exception = ex.ToString() });
+
+ context.Response.ContentType = "application/json";
+ context.Response.StatusCode = (int)code;
+
+ return context.Response.WriteAsync(result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Birdmap/Pages/Error.cshtml b/Birdmap/Pages/Error.cshtml
deleted file mode 100644
index 6f92b95..0000000
--- a/Birdmap/Pages/Error.cshtml
+++ /dev/null
@@ -1,26 +0,0 @@
-@page
-@model ErrorModel
-@{
- ViewData["Title"] = "Error";
-}
-
-Error.
-An error occurred while processing your request.
-
-@if (Model.ShowRequestId)
-{
-
- Request ID: @Model.RequestId
-
-}
-
-Development Mode
-
- Swapping to the Development environment displays detailed information about the error that occurred.
-
-
- The Development environment shouldn't be enabled for deployed applications.
- It can result in displaying sensitive information from exceptions to end users.
- For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development
- and restarting the app.
-
diff --git a/Birdmap/Pages/Error.cshtml.cs b/Birdmap/Pages/Error.cshtml.cs
deleted file mode 100644
index 980f3df..0000000
--- a/Birdmap/Pages/Error.cshtml.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Threading.Tasks;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Mvc.RazorPages;
-using Microsoft.Extensions.Logging;
-
-namespace Birdmap.Pages
-{
- [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
- public class ErrorModel : PageModel
- {
- private readonly ILogger logger;
-
- public ErrorModel(ILogger _logger)
- {
- logger = _logger;
- }
- public string RequestId { get; set; }
-
- public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
-
- public void OnGet()
- {
- RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
- }
- }
-}
diff --git a/Birdmap/Pages/_ViewImports.cshtml b/Birdmap/Pages/_ViewImports.cshtml
deleted file mode 100644
index c37e33d..0000000
--- a/Birdmap/Pages/_ViewImports.cshtml
+++ /dev/null
@@ -1,3 +0,0 @@
-@using Birdmap
-@namespace Birdmap.Pages
-@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
diff --git a/Birdmap/Program.cs b/Birdmap/Program.cs
index 9aa8ff0..9fea490 100644
--- a/Birdmap/Program.cs
+++ b/Birdmap/Program.cs
@@ -1,11 +1,9 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
-using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
+using NLog;
+using NLog.Web;
+using System;
namespace Birdmap
{
@@ -13,7 +11,22 @@ namespace Birdmap
{
public static void Main(string[] args)
{
- CreateHostBuilder(args).Build().Run();
+ var logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();
+
+ try
+ {
+ logger.Debug("Main called...");
+ CreateHostBuilder(args).Build().Run();
+ }
+ catch (Exception ex)
+ {
+ logger.Error(ex, "Exception occurred in Main.");
+ throw;
+ }
+ finally
+ {
+ LogManager.Shutdown();
+ }
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
@@ -21,6 +34,12 @@ namespace Birdmap
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
- });
+ }).ConfigureLogging(logging =>
+ {
+ logging.ClearProviders();
+ logging.AddConsole();
+ logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
+ })
+ .UseNLog();
}
}
diff --git a/Birdmap/Startup.cs b/Birdmap/Startup.cs
index 7683c3f..2043c0b 100644
--- a/Birdmap/Startup.cs
+++ b/Birdmap/Startup.cs
@@ -1,5 +1,6 @@
+using AutoMapper;
+using Birdmap.API.Middlewares;
using Birdmap.BLL;
-using Birdmap.BLL.Interfaces;
using Birdmap.DAL;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
@@ -34,6 +35,8 @@ namespace Birdmap
services.ConfigureBLL(Configuration);
services.ConfigureDAL(Configuration);
+ services.AddAutoMapper(typeof(Startup));
+
var key = Encoding.ASCII.GetBytes(Configuration["Secret"]);
services.AddAuthentication(opt =>
{
@@ -70,9 +73,7 @@ namespace Birdmap
}
else
{
- app.UseExceptionHandler("/Error");
- // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
- app.UseHsts();
+ app.UseMiddleware();
}
app.UseHttpsRedirection();
@@ -86,6 +87,7 @@ namespace Birdmap
app.UseEndpoints(endpoints =>
{
+ endpoints.MapHealthChecks("/health").RequireAuthorization();
endpoints.MapControllers();
});
diff --git a/Birdmap/appsettings.json b/Birdmap/appsettings.json
index f9d3958..d2e0577 100644
--- a/Birdmap/appsettings.json
+++ b/Birdmap/appsettings.json
@@ -8,24 +8,5 @@
},
"AllowedHosts": "*",
"Secret": "7vj.3KW.hYE!}4u6",
- "LocalDbConnectionString": null,
- "Default": {
- "Users": [
- {
- "Username": "user",
- "Password": "pass",
- "Role": "User"
- },
- {
- "Username": "admin",
- "Password": "pass",
- "Role": "Admin"
- }
- ],
- "Endpoints": [
- "",
- "",
- ""
- ]
- }
+ "LocalDbConnectionString": "Data Source=DESKTOP-A6JQ6B5\\SQLEXPRESS;Initial Catalog=Birdmap;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
diff --git a/Birdmap/nlog.config b/Birdmap/nlog.config
new file mode 100644
index 0000000..2ad336d
--- /dev/null
+++ b/Birdmap/nlog.config
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file