asp.net mvc 3 - MVC3: proper place to add a dependency on a logger class? -


I have an MVC3 app with a simple log service. Use Microsoft's Unity Dependency Injection Container to access all of my services is applied.

For most of the ways, I ignore exceptions; They are caught in the top-level error handler, which classifies them and decides whether they want to log on or not, causes HTTP response and I return the action method on the error controller to your custiom error page Call me

Sometimes, though, I do not want to do this; I want to handle the exception where this happens, e.g. In that case in my controller, I want to log the error before replacing a proper default value and continuing with the argument of the controller.

I did the same place: In my controller, I said:

  var logService = DependencyResolver.Current.GetService & lt; ILogService & gt; (); Try {/ * hold some uncertain * /} (exception pre) {logService.LogException (category, pre); / * Do something else instead * /}   

Now I want to do this second time in that controller (as it happens, later in the same method). As soon as I do it again, I see that this time is in the form of refactor, because I repeat myself.

What's the best way to make my logger available to my controller? My controllers come from all custom controllables; My first idea is to connect the creator of the controller but:

  1. I do not currently have a constructor in the controllerbase,
  2. I'm a little worried that the controller in the DI Container Referencing breaks the isolation controller, starting with the value of DI, and
  3. I do not think that I can pass the logger to the controller's controller because (as I think , Me Right if I guess wrong) controllers can have only Paramitrles constructor, so they have no way to pass.

    Where is the appropriate place to provide service for all my administrators?

    Put it in your constructor. You can also put it in BaseController .

    Ideally, you will use dependency injection and it will come to your controller. Controllers may be are parameters if your IoC container supports it.

      Private Readonly ILogService logService; Public MyController (ILogService logService) {this.logService = logService; }    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -