From 8f4880d7ad2c793c451dc80dc9a35adb37bb0f3c Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 29 Oct 2022 14:48:02 -0500 Subject: AƱadidos filters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/AuthorsController.cs | 4 +++- Controllers/BooksController.cs | 4 +++- Filters/ActionFilter.cs | 21 +++++++++++++++++++++ Program.cs | 2 ++ wwwroot/Log.txt | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 Filters/ActionFilter.cs 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 log; + + public ActionFilter(ILogger 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(opt => opt.UseNpgsql(builder.Confi builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddHostedService(); +builder.Services.AddScoped(); 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... + -- cgit v1.2.3