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.");
  • 1
    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???????????" – Blake 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 .. – Blake Nov 1 '12 at 18:44
  • 2
    look into the "cross cutting concern" concepts.. – hanzolo Nov 1 '12 at 18:45

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.

| improve this answer | |

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.

| improve this answer | |

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.

| improve this answer | |
  • Thanks, 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???????????" – Blake Nov 1 '12 at 18:31
  • 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() ? – Blake Nov 1 '12 at 18:44

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.

| improve this answer | |
  • I'm glad this video was posted.. definitely good content in there – hanzolo Nov 1 '12 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.