Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in Razor view in MVC3

How to add logging in razor views? I would like to add debug statements using logging tool such as log4net?

like image 321
SamJackSon Avatar asked Dec 22 '22 12:12

SamJackSon


2 Answers

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.

like image 80
SamJackSon Avatar answered Dec 28 '22 07:12

SamJackSon


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.

like image 43
jgauffin Avatar answered Dec 28 '22 07:12

jgauffin