Take the 2-minute tour ×

Working on a C# WinForms program that is written in a MVC ( actually Model-View-Presenter) style and I want to add a few lines of code that is responsible for logging some events. Where should I write two or three lines of code that I need? Should I write it in the Presenter section?

To get an idea, here is some lines of sample code that already exists in the Save() metohd in Company.MyApplication.Presenter.MyPresenter.cs class:

he has written codes lie the following in this part of presenter:

private void Save(Helper.SaveStatusEnum status)
    if (notification.CheckLocks(orderIdCollection))
        using (new HourglassController())

            int result = saveController.Save(status);

            if (result == Helper.SAVE_SUCCESSFUL)
                //  IS IT OK TO WRITE MY COUPLE LINES OF CODE IN HERE???????????
                model.Dirty = false;

                if ((model.CurrentStatus == Helper.OrderStatusEnum.Complete) || (model.CurrentStatus == Helper.OrderStatusEnum.Corrected))

                MessageBox.Show("An unexpected error occuring trying to save.");
share|improve this question
I would say anywhere but the presenter, unless you're trying to debug something specifically in the presenter. –  Robert Harvey Nov 1 '12 at 18:14
@RobertHarvey : Why? I also updated my question with the exact place that I am thinking of adding my logging. please take a look and see if looks good..look for "// IS IT OK TO WRITE MY COUPLE LINES OF CODE IN HERE???????????" –  BDotA Nov 1 '12 at 18:32
You can write to the log anywhere you want. Why are you asking for permission? :/ –  Robert Harvey Nov 1 '12 at 18:35
@RobertHarvey : I want to make sure I am adding code to the Right place..model ..view .. presenter .. –  BDotA Nov 1 '12 at 18:44
look into the "cross cutting concern" concepts.. –  hanzolo Nov 1 '12 at 18:45
add comment

4 Answers

up vote 9 down vote accepted

Logging is a cross-cutting concern. There is no right place for logging; you do it where you need to.

MVC is agnostic in that regard; it has nothing to say about where you put lines of code that call your logger.

share|improve this answer
add comment

It depends on the goal you have with the logging. If the thing you want to log is a successful Save exactly and only at the position in your code you marked above, then it will obviously be the right place. But if you want to log every sucessful call to saveController.Save in your code, you better place the logging call within that method.

And don't think too much about the MVC architecture with logging - logging is a cross-cutting concern, which can make sense in any layer, depending on what you want to achieve. For example, if you are logging for debugging purposes, it could even make sense in the view layer.

share|improve this answer
add comment

I recommend watching Putting your controllers on a diet by Jimmy Bogard. It's for MVC 2, but it's still very relevant in MVC 4. It's how I now code my MVC applications. It's simple and it creates separation of concerns.

Video Covers:

  • Dependency Injection
  • AutoMapper
  • Model Binding
  • Action Results

You can squeeze in logging wherever you want.. but the patterns described in the video result in efficient MVC development..

The beauty of the approach is it eliminates the nested if statements commonly found in validation and data-retrieval code in an application entry point (i.e. button clicks)


I just realized the question is for MVP in Windows Forms, I still believe that many of the idea's in Jimmy's video apply.

share|improve this answer


tl;dw? (too long, didn't watch?) Please summarize the salient points here. See also meta.stackexchange.com/questions/8231/… –  Robert Harvey Nov 1 '12 at 18:36
@RobertHarvey I disagree with the meta post referenced in your previous comment. –  Chuck Conway Nov 1 '12 at 18:39
I'm glad this video was posted.. definitely good content in there –  hanzolo Nov 1 '12 at 18:40
@ChuckConway While you are free to disagree, answers that do not provide sufficient explanation may be removed at any time. Please update your answer to include the key points of the video, preferably the ones that you succesfully applied to your code. –  Yannis Rizos Nov 1 '12 at 18:50
I know. It's a fantastic presentation, that's why I'm trying to get you to tell us more about it - you'll get tons of rep, I'll get a great answer for the site (win-win). Don't describe the presentation if you feel that's too much, but do tell us how you applied its key points in your code. –  Yannis Rizos Nov 1 '12 at 19:02
show 1 more comment

Probably the model would be the best place since it is the closest part to the system, and logging is done on the system (usually).

View and Presenter are more likely responsible with showing data to user and handling user actions.


If you want to add logging just for ad-hock debugging and than immediately remove it so that it doesn't get into production you can write it to wherever you want. However if I would want to add long-term logging I would add it in model.

But I don't see these things as absolute bad or good solutions. If you have no way, for some well understood reasons, to add it to the models you can add it to the presenter also. Probably the worst place the log could be is the view, so avoid it as much as you can.

share|improve this answer
Thanks...so you mean I write a method in the Model that does the logging...then in the present just call it? like model.DoLogging() ? –  BDotA Nov 1 '12 at 18:44
add comment

Your Answer


