Where is it permissible to add logging code in a MVC model?

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())
        {
            controller.FireActiveCellLeaving();
            ViewDocumentedValues();

            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))
                {
                    controller.EnableDisableSheet(false);
                }

                CheckApplicationState();
                SheetHelper.ClearUnsavedDataRowImage(view.ActiveSheet);
            }
            else
            {
                MessageBox.Show("An unexpected error occuring trying to save.");
            }
        }
    }
}

5

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.

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.

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.

EDIT:

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.

2

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)

update

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật