Solve Logging as a Cross-Cutting Concern with MediatR

  Рет қаралды 28,282

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер: 101
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@3m3rs0n
@3m3rs0n Жыл бұрын
It's perfect how you go right to the point and make it easy to understand. Good job Milan!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Great, good to know that 😁
@sergiodurval715
@sergiodurval715 Жыл бұрын
Your video helped me a lot I'm studying cqrs with mediator and I intend to use this example log in my project Thanks for sharing knowledge
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad it was helpful!
@ThrottleScotty
@ThrottleScotty Жыл бұрын
Excellent video, as usual. I really like the format, succinctness and detail. Keep up the good work Milan!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you, it's going to become even better this year
@fernandocalmet
@fernandocalmet Жыл бұрын
Excellent topic, I was just thinking in the last days how to implement logging with Mediatr. Thank you very much Milan
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
There you go! 😁
@nove1398
@nove1398 Жыл бұрын
Great video. Similar approach to making exception handling.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Right! Middleware solves many problems
@vu5700
@vu5700 Жыл бұрын
Amazing video sir, i really enjoy your videos , very concise and and concrete, keep it up!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you very much!
@Adronius
@Adronius Жыл бұрын
I really enjoy your videos! Short, to the point, with great value. It would be great to see the repository. I'd like to learn more on the Result implementation and how the other MediatR classes looks like. Like for example the UpdateProductCommand. Btw, get also some free time, so that you don't burn out. Working on Christmas day is not a good idea ;-)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I covered most of these in some of the previous videos - UpdateProductCommand -> Minimal API Organization - Result -> Validation from Aug/Sep - Repository -> Repository Pattern video
@kwwx345
@kwwx345 Жыл бұрын
It would be nice to see you make a video on other ways to manage cross cutting concerns like aspect oriented programming with postsharp(or new metalama) or other similar tools.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Very interesting, I'll have to add this to my content list! 😁
@thomaswoods1365
@thomaswoods1365 4 ай бұрын
Really nice Milan. Thank you!
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Most welcome
@richardhaughton9633
@richardhaughton9633 Жыл бұрын
Great video again! Could you please make one on logging with ELK stack (Elastic search, LogStash and Kibana) ?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Absolutely! I want to cover all the hot topics, but I want to make the videos easy to consume (short) so I can't tackle everything in one video.
@oyedeoluwafunbi9635
@oyedeoluwafunbi9635 Жыл бұрын
@@MilanJovanovicTech I am totally looking forward to this!!
@bugadada
@bugadada Жыл бұрын
Much respect for what you do, keep going!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I appreciate that!
@MarcusKaseder
@MarcusKaseder Жыл бұрын
Why do you add an @ in front of your placeholders? One thing to mention for logging in general: Even if the logs are disabled by config, the cost is still there. If we assume that DateTime.UtcNow will take 1 second to execute, it will also take one second when the log level is disabled. There are some workarounds to avoid this. One is a delegate that only executes if the log level is enabled. Another one is to nest your log into a if that checks the log level and only executes if log level is enabled. Wanted to mention that because some traces can be cost intensive. That traces are mostly not required in production mode.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Just a convention I saw somewhere with "@", but it seems it's not even needed. Great point about the cost of non-logged logs, I'll have to bring this up in a future video!
@MarcusKaseder
@MarcusKaseder Жыл бұрын
@@MilanJovanovicTech Ah, okay. Wondered if I missed something. It's always about personal preferences 😉 Since those placeholders are reflected by structured logging in JSON or logging targets (logging DBs), it personally feels strange to me to query for @ItemId = 1 instead of ItemId = 1. Great Video as always 🙂
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
@@MarcusKaseder From my testing, they are excluded in logs. At least with Serilog
@dariogriffo
@dariogriffo Жыл бұрын
Marcus, the @ is identified in "THE" best structured logging library: Serilog as the way to render an object as JSON in your log properties. My suggestion is DONT use Microsoft logging, but use Serilog directly. Microsoft is an abstraction an misses a lot of what you can do with the vendor specific.
@MarcusKaseder
@MarcusKaseder Жыл бұрын
@@MilanJovanovicTech @Darío Found the following: The @ symbol before the segment argument is a part of the Message Template specification which means that Serilog must expand the argument's structure as an object. Without this symbol Serilog would just log the result of segment.ToString(). Segment is a class I that case.
@zikkrype
@zikkrype Жыл бұрын
Nice one. For me the biggest problem is always splitting LogEvent ID between different application layers (e.g: new EventId(100, "Unhandled"), new EventId(1001, "Payments"))
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I think that can be handled very nicely by Serilog's LogContext
@zikkrype
@zikkrype Жыл бұрын
@@MilanJovanovicTech that's not what i'm talking about. later you filter your logs in kibana or application insights using event(id, name). How to avoid collisions of ids etc
@syedemad1070
@syedemad1070 Ай бұрын
In vsa will conventional middleware for logging work with mediatr?
@MilanJovanovicTech
@MilanJovanovicTech Ай бұрын
Yes
@shirinmonzavi8165
@shirinmonzavi8165 8 ай бұрын
Hi How to extract information about what handler is handled the request?
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
I'm guessing the RequestHandlerDelegate should have that info somewhere
@ransandu
@ransandu Жыл бұрын
Hi Milan, Thanks for this. Small clarification. The implementation you did is only covering all the inbound request/response. How about logging outbound request/response. E.g Assume we calling a 3rd party application. Want to record what we send and receive in our logs. Any thought on this?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
If you're calling that API via HttpClient, you can create a delegating handle and introduce the logging in there
@abdulatifrasulov7291
@abdulatifrasulov7291 8 ай бұрын
Good job. But i have one question. Is this behavior effect API performance if this API should serve large number of requests?
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
Logging will incur a performance penalty, but not knowing what your app is doing is a bigger problem.
@ederjuniordesouza1618
@ederjuniordesouza1618 Жыл бұрын
Hello Milan great video, thank you for sharing :) I was expecting to see something about MediatR, is it coming in a next video or did I miss something in this video ?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Check out this maybe: kzbin.info/www/bejne/rJXMXqNviNmgfZI
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Also, validation with MediatR: kzbin.info/www/bejne/bmbHqaqaba2te80
@volodymyrliashenko1024
@volodymyrliashenko1024 Жыл бұрын
I was expecting to see structure logs as a result and than how to use it and what the benefits of having Structured Logs vs plain logs.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I'm planning that for the Serilog video which is coming soon
@volodymyrliashenko1024
@volodymyrliashenko1024 Жыл бұрын
@@MilanJovanovicTech Would be nice if you can recommend application for viewing structured logs locally.
@fahimehbarzegar5322
@fahimehbarzegar5322 Жыл бұрын
Is it just about log in application layer? How can we use this approach to log infrastructure layer, such as repository? Or what is the best way to log them?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You can add a decorator to your repositories, or log within command handlers if you feel you need go
@BaffourKTonto
@BaffourKTonto Жыл бұрын
Is Logging the same as having auditing to keep information about which user performed an operation?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
They serve a different purpose. Auditing is storing data for historical reference. Logs are usually short-lived and are only relevant for a few days.
@murilomurilo3930
@murilomurilo3930 Жыл бұрын
Where the connection string is stored? I like your videos! ❤
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Appsettings.json
@alpsavasdev
@alpsavasdev 7 ай бұрын
what are the disadvantages of logging in application layer, compared to in API layer (in controller methods)?
@MilanJovanovicTech
@MilanJovanovicTech 6 ай бұрын
You can do both
@ManojRawat-lp4yc
@ManojRawat-lp4yc 2 ай бұрын
Is it a good practice to create this pipeline behavior handling class for every .NET core project where we need to log information and errors ? Or is it something specific to MediatR only ? Like if I want to do this for Serilog DB log sinking ? Will it be good design ? Edit ** After going through the Video one more time** I think we can use it anywhere not just with MediatR. But if possible please confirm. Thank You.
@MilanJovanovicTech
@MilanJovanovicTech 2 ай бұрын
Pipeline behavior is a MediatR feature. You could implement a middleware if you need this in Web apps.
@vinland-h1w
@vinland-h1w Жыл бұрын
I'm using NLog and I'm confused, It's already in the API project, should I do the same in Application layer? Also, how can this affect the flow if I have validatorsbehavior too. I hope u can respond thanks a lot!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I think it's fine to keep it in the Application layer.
@vinland-h1w
@vinland-h1w Жыл бұрын
@@MilanJovanovicTech thank you so much, this helps a lot
@sauravbhatta5303
@sauravbhatta5303 Жыл бұрын
Precise info
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you!
@ahsanmehmoodqureshi7035
@ahsanmehmoodqureshi7035 Жыл бұрын
please tell the model domain.shared =Result .which fields inside in it
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
gist.github.com/m-jovanovic/aa25b1ae424c985ff8ae696a79b6fe6e
@winstochurgle9133
@winstochurgle9133 7 ай бұрын
Yo Milan. Thanks for video. I can't enter into Handle method of logging pipeline. Any thoughts? Here's my implementation (i'm using FluentResults): public class LoggingPipelineBehavior : IPipelineBehavior where TRequest : IRequest where TResponse : Result public class CategoryQueryHandler : IRequestHandler, IRequestHandler
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
The problem is: where TResponse : Result Try out with just Result (if it exists) or an interface
@winstochurgle9133
@winstochurgle9133 7 ай бұрын
@@MilanJovanovicTech Well i tried like this: public class LoggingPipelineBehavior : IPipelineBehavior where TRequest : IRequest where TResponse : IResultBase Because Result inherits IResultBase and it's worked. Thanks!
@aslanamca8225
@aslanamca8225 Жыл бұрын
I see cqrs and mediatR even when I want to learn another subject. I hate this situation.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
If you can't beat them, join them?
@gerarduab9960
@gerarduab9960 Жыл бұрын
Thanks. But how to read logs in production environment during certain period of time?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Where's your production environment?
@krccmsitp2884
@krccmsitp2884 Жыл бұрын
In production you would store the logging messages in a centralized logging store, e.g. Seq, Logstash, SolarWind, or a cloud storage system.
@pbolduc
@pbolduc Жыл бұрын
@@krccmsitp2884 We use Splunk via Serilog. But I understand what Gerard is asking. For example, a user is reporting an error, but the existing log levels are not verbose enough. It would be useful to be able to turn on more detail logging for a period of time. Previously with trace listeners, you configure your trace listener with a trace switch. You could programmatically at run time change the trace level. You could have an administrative endpoint that you could call to change the level to Verbose (Debug in .NET Core ILogger), and start a timer to reset the level to Information after a short period of time like 5 minutes. I also have use case where I would like to enable logging for one or more users. Although both of these situations do not work well if you are running multiple instances such as on Kubernetes.
@gerarduab9960
@gerarduab9960 Жыл бұрын
@@pbolduc that's. Always I listen to add ilogger to logging user data, errors, but who I turn that in order to make it more practice. I think that this data should save it in some kind of log store but during certain periods of time, also this data should be filtered in order to extract metrics. My question it's that, what infrastructures, approach are used in production environments.
@CyborgT800
@CyborgT800 Жыл бұрын
Can you cover Microsoft App insights logging
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Good idea! I'll add it to my queue
@10Totti
@10Totti Жыл бұрын
Thanks code is on github?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
www.patreon.com/milanjovanovic
@rubenguinez92
@rubenguinez92 Жыл бұрын
I tested this and the content of class LogginPipelineBehavior is not triggered. I really don't know what happened. Of course I've set up DI and the packages installed. (NET 7)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Did you setup the pipeline with MediatR?
@rubenguinez92
@rubenguinez92 Жыл бұрын
@@MilanJovanovicTech AddMediatr and IPipelineBehavior registered in services/Program.cs, just like you do in the video…
@rubenguinez92
@rubenguinez92 Жыл бұрын
@@MilanJovanovicTech In the video, do you use net 7?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
@@rubenguinez92 Yes
@MateStier-hd4nv
@MateStier-hd4nv 9 ай бұрын
I would like to log Result.Value, wow can i implement it?:D
@MateStier-hd4nv
@MateStier-hd4nv 9 ай бұрын
I Can with reflection, but not nice PropertyInfo? valueProperty = result.GetType().GetProperty("Value"); object? propValue = valueProperty?.GetValue(result, null); _logger.LogInformation( "Handle Complited {@RequestName}, {@value}", reuqestType.Name, propValue);
@MateStier-hd4nv
@MateStier-hd4nv 9 ай бұрын
I would like to make it type safety! Is it possible?:D
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
Why not simply log... Result.Value - what's preventing you?
@MateStier-hd4nv
@MateStier-hd4nv 9 ай бұрын
​@@MilanJovanovicTech When add constrain: public class LoggingPipelineBehavior : IPipelineBehavior where TRequest : IRequest where TResponse : Result Mediatr dont use it. I take breakpoint this class , but dont stop -> dont use it. Only use when i use just where TResponse : Result. i want to use Result implementation
@Maorprog
@Maorprog Жыл бұрын
Great tutorial. can you please share with us the code in github? Thanks!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I only share the code with my Patreon supporters
@Alexandrplayable
@Alexandrplayable Жыл бұрын
How to write this if my Result class write how Result??? help meee
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
gist.github.com/m-jovanovic/aa25b1ae424c985ff8ae696a79b6fe6e
You are doing .NET logging wrong. Let's fix it
25:29
Nick Chapsas
Рет қаралды 174 М.
Бенчик, пора купаться! 🛁 #бенчик #арти #симбочка
00:34
Симбочка Пимпочка
Рет қаралды 4,1 МЛН
ПРЯМОЙ ЭФИР. Золотой мяч France Football 2024
4:41:06
Não sabe esconder Comida
00:20
DUDU e CAROL
Рет қаралды 59 МЛН
How To Implement Validation With MediatR And FluentValidation
18:33
Milan Jovanović
Рет қаралды 46 М.
C# Logging with Serilog and Seq - Structured Logging Made Easy
1:07:56
You DON'T want an In-Memory Event Bus like MediatR
11:20
CodeOpinion
Рет қаралды 22 М.
Making A WebSocket Server With .NET 8🧑‍💻  [FULLSTACK 2024 VIDEO 1]
18:43
Alex's Dev Den 👨‍💻
Рет қаралды 13 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 868 М.
The MediatR Feature NOBODY Is Using!
12:38
Codewrinkles
Рет қаралды 4,1 М.
Why I DON'T use MediatR in ASP.NET Core
21:51
Raw Coding
Рет қаралды 15 М.
The Logging Everyone Should Be Using in .NET
15:34
Nick Chapsas
Рет қаралды 78 М.
Serilog 🚀 Logging in .NET 7 Made Simple & Fun
16:23
Patrick God
Рет қаралды 46 М.
Бенчик, пора купаться! 🛁 #бенчик #арти #симбочка
00:34
Симбочка Пимпочка
Рет қаралды 4,1 МЛН