How to add logging in razor views? I would like to add debug statements using logging tool such as log4net?
I agree with the other replies but this would be for temparary debugging such finding out count or similar scenario.
After fiddling around a little, following syntax worked.
@{ ViewBag.Log = log4net.LogManager.GetLogger("Products.cshtml");}
@ViewBag.Log.Debug("Products count = " + Model.Products.ToList().Count);
Hope this will help someone.
I strongly discourage you from doing that.
It sounds to me that you have logic in your views. It makes the views hard to maintain and even harder to test the logic.
Move the logic either to your controllers or your view models. And log in those instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With