jasontaylordev / CleanArchitecture

Clean Architecture Solution Template for ASP.NET Core
MIT License
16.13k stars 3.45k forks source link

LoggingBehaviour in the Application project doesn't do anything #1188

Open Genoe opened 1 month ago

Genoe commented 1 month ago

Describe the bug The LoggingBehaviour class and its Process method do execute when a request is made.

To Reproduce Steps to reproduce the behavior:

  1. Set up a new project. I used the Angular template with SQLite
  2. Set a breakpoint on line 31 in LoggingBehaviour.cs (or anywhere in the Process method)
  3. Do something in the app that fetches data. For example, go to the weather data page.
  4. Observe that the breakpoint was not hit, the app does not log the user and their request

Expected behavior The breakpoint should be hit and we should log information about the user and their request.

Additional context Unlike the other behaviors in Application/Common/Behaviours, LoggingBehaviour implements IRequestPreProcessor. According to the Mediatr docs, "RequestPreProcessorBehavior will execute IRequestPreProcessor implementations before any handlers are called". However, the docs also say this "will be registered if in AddMediatR you register any pre/post processors". So it's not clear to me if a step is missing. I'm guessing cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()); in Application/DependencyInjection.cs is supposed to take care of this. But I'm really not sure.

EDIT: Adding cfg.AddRequestPreProcessor(typeof(IRequestPreProcessor<>), typeof(LoggingBehaviour<>)); after cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()); registers LoggingBehaviour and then the breakpoint can be hit. However I'm guessing that is not the intended way for it to work.