Elmah - ASP.NET & # 8594; Multiple Connection Strings & # 8594; Set SQL error log connection string in code

I want to integrate ELMAH into an existing ASP.NET application to further support error research and can help with the connection string. We use a single web.config file for all or our environments in which the application is deployed, and at runtime the application decides which environment it is located in, usually based on the URL.

This is what we will like the standard block ...

  <connectionStrings>
    <add name="TESTAppDB" connectionString="Data Source=SQL-T-APPNAME.COMPANY.COM;Initial Catalog=APPNAME;User ID=USER;Password=THEPASS" providerName="System.Data.SqlClient"/>
    <add name="CERTAppDB" connectionString="Data Source=SQL-C-APPNAME.COMPANY.COM;Initial Catalog=APPNAME;User ID=USER;Password=THEPASS" providerName="System.Data.SqlClient"/>
    <add name="PRODAppDB" connectionString="Data Source=SQL-P-APPNAME.COMPANY.COM;Initial Catalog=APPNAME;User ID=USER;Password=THEPASS" providerName="System.Data.SqlClient"/>
  </connectionStrings>

With Elmah, it seems you just need to specify the connection string name, but how can I do this dynamically at runtime? For example, if I test, then I want this:

<elmah>
     <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="TESTAppDB"/>
</elmah>

but if I'm in PROD:

<elmah>
     <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="PRODAppDB"/>
</elmah>


- , . , ELMAH Sql...

- . , , TEST, CERT. CERT PROD. - , ...

+3
5

Elmah , : -)

, , SqlErrorLog, , , SqlErrorLog.ConnectionString, , .

- ( - ):

public static string CustomConnectionString { get; set; }

public virtual string ConnectionString
{
  get 
  { 
    if (string.IsNullOrEmpty(CustomConnectionString)) 
    {
      return _connectionString;
    }
    else
    {
      return CustomConnectionString; 
    }
  }
}

. , , , , , SqlErrorLog.CustomConnectionString.

+4
+4

"AppDB", ( web.config), .

web.config:

<connectionStrings configSource="config\connectionStrings.config"/>

connectionStrings.config:

<connectionStrings>    
    <add name="AppDB" connectionString="Data Source=SQL-T-APPNAME.COMPANY.COM;Initial Catalog=APPNAME;User ID=USER;Password=THEPASS" providerName="System.Data.SqlClient"/>
</connectionStrings>

, , connectionStrings.config, connectionString. "AppDB".

0

Rune Grimstad, :

if (connectionString.Length == 0)
    connectionString = CustomConnectionString;

string connectionString = ConnectionStringHelper.GetConnectionString(config);

SqlErrorLog (IDictionary config).

, RSolberg, application_start, :

Elmah.SqlErrorLog.CustomConnectionString = " CS";

0

?

webapp MVC 3, ( ):

using (var ctx = new TESTAppDB())
{
    var res = (from p in ctx.Customer select p).FirstOrDefault();
}

TESTAppDB:

public class TESTAppDB: DbContext
{
    public DbSet<Customer> Customer{ get; set; }
}

, :

using (var ctx = new CERTAppDB())
{
    var res = (from p in ctx.Order select p).FirstOrDefault();
}

, ...

0

Source: https://habr.com/ru/post/1708588/


All Articles