1

I have a MVC project which has Model,DataAccess,Business and View layers. I am intending to use NLog to log exceptions into text files. I am not sure in which layer I should log errors, that is my first question. I read some blogs which recommend to log errors in View layer. I am not sure about that.

Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189
  • if all you are looking to do is trap and log exceptions, use elmah. https://www.nuget.org/packages/elmah/ – Fran Nov 11 '16 at 14:11
  • I don't think there's a single solid answer to this. All you're going to get are opinions. –  Nov 11 '16 at 14:14

1 Answers1

2

I think the best way is to use MVC. You should do logging in one place for common exceptions and the only common place you get is top-level, MVC in your case.

There are special error handlers for MVC that you should utilize/override to get the behavior that you want + global.asax generic error handler for stuff outside of MVC. That will cover exceptions happening in any layer compared to doing it in other places you would have to duplicate it.

As already mentioned in comments you can use Elmah as well. It will pretty much do what I have described, but automatically, you just add an Elmah.Mvc package and set it up to log to a file. You need to be careful not to expose elmah endpoint so that everyone can see your logs.

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
  • 1
    I agree with all of that. I'd also like to add that you don't want to litter your code with a bunch of try/catch block is various layers just to capture exceptions to log them. Also this is don't write this stuff this stuff yourself. there are enough packages out there elmah, Enterprise library with the Exception Manangement Block. Focus on creating business value with your code. – Fran Nov 11 '16 at 14:57
  • 1
    Thought it's kind of obvious that if you have one top-level, you don't need try/catches everywhere, but you are right, had so many troubles with guys thinking try/catch{} is a cool idea and put it in all places. – Ilya Chernomordik Nov 11 '16 at 15:04