Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:46:42+00:00 2026-05-25T10:46:42+00:00

I have an MVC3 app with a simple Log service. All my services are

  • 0

I have an MVC3 app with a simple Log service. All my services are invoked using Microsoft’s Unity Dependency Injection container.

For most methods, I ignore exceptions; they’re caught in a top-level error handler, which categorizes them and decides whether or not to log them, generates the HTTP response I desire and calls an action method on the error controller to return my custiom error page.

Sometimes, tho, I don’t want to do that; I want to handle the exception where it happens, e.g. in my controller. In that case, I want to log the error before substituting an appropriate default value and continuing with the controller’s logic.

I did that in one place: in my controller, I added:

var logService = DependencyResolver.Current.GetService<ILogService>();
try { /* something indeterminate */ }
catch ( Exception ex ) {
    logService.LogException(category, ex);
    /* do something else instead */
}

Now I want to do it a second time in that controller (as it happens, later in the same method). As soon as I do this again, I see it’s time to refactor, as I’m repeating myself.

What’s the best way to make my logger available to my controllers? My controllers all inherit from a custom ControllerBase; my first thought is to add it to the ControllerBase’s constructor. BUT:

  1. currently I don’t have a constructor in the ControllerBase,
  2. I’m a bit worried that referencing the DI container in the
    controller breaks the isolation of the controller, negating the
    value of DI to begin with, and
  3. I don’t think I can pass the
    logger in to the ControllerBase’s constructor, because (as I
    understand it, pls correct me if ‘m wrong) controllers can only have
    parameterless constructors, so there’s no way to pass anything to
    them.

Where’s the proper place to make the service available to all my controllers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:46:43+00:00Added an answer on May 25, 2026 at 10:46 am

    Place it in your constructor. You can even place it in a BaseController.

    Ideally, you will use Dependency Injection and have it come in on your controller. Controllers can have parameters if your IoC container supports it.

    private readonly ILogService logService;
    public MyController(ILogService logService) 
    {
       this.logService = logService;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC3 app with a WCF service reference. Of course when im
I have a simple entity framework query in an MVC3 app, like so var
I'm using MVC3 and have a simple helper that draws a box with some
I have a very simple asp.net mvc3 app that uses jquery::getJSON to call into
Have an ASP.NET MVC3 app with model validation using FluentValidation . User enters some
I have an MVC3 app which was using EF CTP5. After upgrading to EF
i have been using code first technique in ASP.NET MVC3 app. here it is
Using Windows Azure's Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider as the outputCache provider for an MVC3 app. Here is
I have an ASP.NET MVC3 app and when the user clicks on my anchor
I have an MVC3/EF4.1 app that I'm building which now works fine on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.