Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lesson03/StartOfLesson/ToDo/Controllers/ToDoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ namespace ToDoApp.Controllers
{
public class ToDoController : Controller
{



// GET: ToDo
public ActionResult Index()
{


return View(Repository.ToDos);
}

Expand Down
55 changes: 55 additions & 0 deletions Lesson03/StartOfLesson/ToDo/Middleware/LoggerMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ToDoApp.Middleware
{
public class LoggerMiddleware : IMiddleware
{
private readonly ILogger<LoggerMiddleware> _logger;
public LoggerMiddleware(ILogger<LoggerMiddleware> logger)
{
_logger = logger;
}

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{

string path = context.Request.Path;
String[] pathParts = path.Split("/");
string controller = "";
string action = "";
string id = "";

if (pathParts.Length == 4)
{
controller = pathParts[1];
action = pathParts[2];
id = pathParts[3];
}

//should only happen when ToDoController has been called
if (controller == "ToDo")
{
//change the message
//show the controller name, action, and id if available
_logger.LogError("controller = " + controller + ". action = " + action + ". id = " + id);
}






await next(context);
}




}
}
1 change: 1 addition & 0 deletions Lesson03/StartOfLesson/ToDo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;

namespace ToDoApp
{
Expand Down
6 changes: 5 additions & 1 deletion Lesson03/StartOfLesson/ToDo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ToDoApp.Middleware;

namespace ToDoApp
{
Expand All @@ -26,7 +28,7 @@ public void ConfigureServices(IServiceCollection services)
options.MinimumSameSitePolicy = SameSiteMode.None;
});


services.AddTransient<LoggerMiddleware>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

Expand All @@ -45,6 +47,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseStaticFiles();
app.UseCookiePolicy();

app.UseMiddleware<LoggerMiddleware>();

app.UseMvc(routes =>
{
routes.MapRoute(
Expand Down