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 8698757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:47:42+00:00 2026-06-13T01:47:42+00:00

I have an ELMAH custom ErrorLog that uses an EF Code-First context to store

  • 0

I have an ELMAH custom ErrorLog that uses an EF Code-First context to store the errors:-

class EntityFrameworkElmahErrorLog
{
    public EntityFrameworkElmahErrorLog(IDictionary config) : this() { }

    public override ErrorLogEntry GetError(string id)
    {
        using (var context = new MyContext())
        {
            var intId = Int64.Parse(id, CultureInfo.InvariantCulture);
            var item = context.ErrorLog.Single(x => x.Id == intId);
            return new ErrorLogEntry(this, id, ErrorXml.DecodeString(item.Details));
        }
    }

    // etc.

}

The ErrorLog is wired up in the web.config:-

<errorLog type="MyProject.EntityFrameworkErrorLog, MyProject" />

I’m already using Ninject elsewhere in the project. I’d like to inject MyContext so that the ErrorLog isn’t instantiating its own dependency, but I’m not having any luck finding a hook in the documentation. ELMAH appears to be instantiating the ErrorLog internally, so the only option I seem to have is using a ServiceLocator inside my custom ErrorLog, which I’d like to avoid if possible.

Are there any better hooks available in ELMAH that I can use to inject ?

  • 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-06-13T01:47:43+00:00Added an answer on June 13, 2026 at 1:47 am

    The Service location/Depdency injection extension point in ELMAH is the ServiceCenter.Current property where you can provide a delegate with the following signature:

    public delegate IServiceProvider ServiceProviderQueryHandler(object context);
    

    ELMAH will use the System.IServiceProvider returned by the ServiceCenter.Current to resolve the ErrorLog isntances.

    So you need to do 3 things to setup it with Ninject (or any DI container)

    1. Create your own System.IServiceProvider implementation with Ninject the IKernel interface already derives from from System.IServiceProvider, so it’s done.
    2. You need to register your EntityFrameworkElmahErrorLog in your container as an ErrorLog implemenation, because ELMAH will try to resolve an instance of ErrorLog.
    3. Provide your delegate to ServiceCenter.Current

    So you need something like the following in your RegisterServices method:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ErrorLog>().To<EntityFrameworkElmahErrorLog>();
        ServiceCenter.Current = (httpContext) => kernel;
    }  
    

    Note: in in the ServiceProviderQueryHandler delegate you get the current HttpContext and with that you can fine tune how your expediencies are resolved.

    You should also note that with this approach you will lose the ability to configure your ErrorLog in your config file.

    ELMAH will always use the resolved instance from your container, because the built in ServiceContainer reads the config file what you override with your custom logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I have my custom errors disabled, Elmah is taking all exceptions that occur
I'm looking at using ELMAH for the first time but have a requirement that
I have an ASP.NET MVC site and ELMAH is showing me that my site
I have a C# web forms ASP.NET 4.0 web application that uses Routing for
I have custom errors turned on in webconfig and redirecting to /Error/Trouble. This is
I have an ASP.NET MVC 3 web site that uses Windows Authentication running under
I have configured ELMAH to catch the errors. When I browse to the localhost:portno/elmah.axd,
Let's say that I have 5 apps that I want to add ELMAH logging
In the current application I am working on I have a custom ControllerFactory class
I'm trying to setup ELMAH to log errors for our application. I have successfully

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.