Elmah-ASP.NET ->複数の接続文字列->設定のSQLエラーログに接続文字列コード

StackOverflow https://stackoverflow.com/questions/878987

質問

い統合 ELMAH 既存のASP.NET 応用もより一層の配慮をするために、エラー調査を使うことができた一部の接続文字列です。またモーバイルコンピューティング。設定ファイルのすべてまたは当社の環境に配備され、実行時には、アプリを決める環境では、一般的に基づきます。

この標準ブロックの皆様のために利用させていただきます。

  <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>

とElmahうだけの名前を指定し、接続文字列がする方法を教えてくださいここを動的に実行時?例えば、私が試したいこと:

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

ものについて知っておきましょPROD:

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

編集
の展開の実践のためのwebアプリケーションは対象外とな思いです。いコードのソリューションを、データソースのELMAH Sqlエラーログに...

できない方法の変更に展開していますwebアプリです。これは、あの試験では、CERT.うCERT動新世紀を展望して"Webアプリが必要を決定することができる環境で実行していな---

役に立ちましたか?

解決

以来、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 物件です。

他のヒント

を使用したいWeb展開プロジェクトです。を指定できるファイルは、接続文字列に対してそれぞれの設定(デバッグおよびリリースによるデフォルト)。このように全ての設定ファイルのすべての展開が同じファイルやweb.configは適宜更新された各展開。

スコットGuは、良い情報はこちら http://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx

では以前のリリース情報がまだ適用されます。

混雑して待たされることを一つの接続文字列名"AppDB"が、実際にホストの接続文字列を外部の外側ます。configファイルのconfigファイルにはそれぞれの環境です。

おます。config:

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

その後、connectionStrings.設定ファイル:

<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.設定ファイルは変更にconnectionString.のコードだけを参照して"AppDB".

を"ルーンGrimstadの答えは、彼の変更、追加:

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

変更後:

string connectionString = ConnectionStringHelper.GetConnectionString(config);

のSqlErrorLog(IDictionary config)コンストラクタ。

そんRSolbergかったが、application_startに対してセッション:

Elmah.SqlErrorLog.CustomConnectionString="おCS";

利用できるのでこの場合データベース。

私はwebappのMVC3に接続すデータベースのデータベースの文脈において(上記例):

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();
}

そうする正しい...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top