summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Controllers/AuthorsController.cs4
-rw-r--r--Controllers/BooksController.cs4
-rw-r--r--Filters/ActionFilter.cs21
-rw-r--r--Program.cs2
-rw-r--r--wwwroot/Log.txt36
5 files changed, 65 insertions, 2 deletions
diff --git a/Controllers/AuthorsController.cs b/Controllers/AuthorsController.cs
index 647a7e5..8d08137 100644
--- a/Controllers/AuthorsController.cs
+++ b/Controllers/AuthorsController.cs
@@ -1,8 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using LibraryAPI.Models;
+using LibraryAPI.Filters;
namespace LibraryAPI.Controllers {
+ [ServiceFilter(typeof(ActionFilter))]
[Route("api/authors")]
[ApiController]
public class AuthorsController : ControllerBase {
@@ -78,4 +80,4 @@ namespace LibraryAPI.Controllers {
return Ok();
}
}
-} \ No newline at end of file
+}
diff --git a/Controllers/BooksController.cs b/Controllers/BooksController.cs
index d238da7..9b744f8 100644
--- a/Controllers/BooksController.cs
+++ b/Controllers/BooksController.cs
@@ -1,8 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using LibraryAPI.Models;
+using LibraryAPI.Filters;
namespace LibraryAPI.Controllers {
+ [ServiceFilter(typeof(ActionFilter))]
[Route("api/books")]
[ApiController]
public class BooksController : ControllerBase {
@@ -89,4 +91,4 @@ namespace LibraryAPI.Controllers {
return Ok();
}
}
-} \ No newline at end of file
+}
diff --git a/Filters/ActionFilter.cs b/Filters/ActionFilter.cs
new file mode 100644
index 0000000..002adda
--- /dev/null
+++ b/Filters/ActionFilter.cs
@@ -0,0 +1,21 @@
+using Microsoft.AspNetCore.Mvc.Filters;
+using Microsoft.AspNetCore.Mvc;
+
+namespace LibraryAPI.Filters {
+ public class ActionFilter : IActionFilter {
+ private readonly ILogger<ActionFilter> log;
+
+ public ActionFilter(ILogger<ActionFilter> log) {
+ this.log = log;
+ }
+
+ public void OnActionExecuting(ActionExecutingContext context) {
+ string log_string = $@"Got request {context.HttpContext.Request.Method} {context.HttpContext.Request.Path}";
+ log.LogInformation("Before Action\n" + log_string);
+ }
+
+ public void OnActionExecuted(ActionExecutedContext context) {
+ log.LogInformation("After Action");
+ }
+ }
+}
diff --git a/Program.cs b/Program.cs
index 3abbb4e..d717af7 100644
--- a/Program.cs
+++ b/Program.cs
@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using LibraryAPI.Models;
+using LibraryAPI.Filters;
using LibraryAPI.Services;
var builder = WebApplication.CreateBuilder(args);
@@ -9,6 +10,7 @@ builder.Services.AddDbContext<LibraryContext>(opt => opt.UseNpgsql(builder.Confi
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHostedService<LoggerService>();
+builder.Services.AddScoped<ActionFilter>();
var app = builder.Build();
diff --git a/wwwroot/Log.txt b/wwwroot/Log.txt
index 8e493c4..ea20df8 100644
--- a/wwwroot/Log.txt
+++ b/wwwroot/Log.txt
@@ -2,3 +2,39 @@ Executing task
25/10/2022 08:42:33
Stopping task...
+Executing task
+26/10/2022 06:19:19
+Stopping task...
+
+Executing task
+26/10/2022 07:24:05
+Stopping task...
+
+Executing task
+26/10/2022 07:25:33
+Stopping task...
+
+Executing task
+26/10/2022 07:27:22
+Stopping task...
+
+Executing task
+26/10/2022 07:30:33
+Stopping task...
+
+Executing task
+26/10/2022 07:30:39
+Stopping task...
+
+Executing task
+26/10/2022 07:32:20
+Stopping task...
+
+Executing task
+26/10/2022 07:32:58
+Executing task
+26/10/2022 07:37:58
+Executing task
+26/10/2022 07:42:58
+Stopping task...
+