asp.net-coreconnection-stringelmah

How can I get the connection string from configuration in the InjectsAuthElements methods in Startup.cs


I am trying to configure elmah with sql server. The configuration I see are of this form

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        ...
    }
    private static void InjectsAuthElements(IServiceCollection services)
    {
        ...
        services.AddElmah<SqlErrorLog>(options =>
        {
            options.Path = "ElmahNewNameForSecurity.axd";
            options.OnPermissionCheck = context => context.User.Identity.IsAuthenticated && context.User.IsInRole("Admin");
            options.ConnectionString = "MyConnectionString";
        });
    }
}

I want to replace MyConnectionString by something like Configuration.GetConnectionString("Default").


Solution

  • Just add an IConfiguration interface as a second parameter to the method and pass the Configuration when you call the method.

    private static void InjectsAuthElements(
        IServiceCollection services,
        IConfiguration configuration)
    {
        ...
        services.AddElmah<SqlErrorLog>(options =>
        {
            options.Path = "ElmahNewNameForSecurity.axd";
            options.OnPermissionCheck = context => context.User.Identity.IsAuthenticated && context.User.IsInRole("Admin");
            options.ConnectionString = "MyConnectionString";
        });
    }