开发者

Logging in Razor view in MVC3

How to add logging in razor views? I would like to add debug statements usin开发者_运维百科g 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.


Sounds like you might be better off with an application wide logging approach as you don't want to catching logic errors in your view. Logic should be in controllers and (to an extent) models.

Have a look here -> https://stackoverflow.com/questions/5363794/what-does-an-mvc-basecontroller-with-logging-look-like to see how to log errors and catch exceptions throughout your site.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜