1

私は非常に多くのレベルで罪を犯しました。誰かがこのc#を書き直すためのより良い方法を教えてくれることを願っています。

実行時にweb.configのセクションを変更して、elmahエラー電子メールの件名の一部を削除し、ボックス名を挿入するタスクが与えられました。

その理由は、cmの人々がこれらを一貫して正しく取得することを信頼できないため、間違ったボックスでエラーをデバッグするのに時間を浪費するからです。

だから私の前にその醜さで私は書き始めました...

これが私が変更しようとしているweb.configのセクションです

 <elmah>
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ELMAH"  />
    <errorMail from="..." to="..."
      subject="Application: EditStaff_MVC,  Environment:Dev, ServerBoxName: AUST-DDEVMX90FF"
      async="true" />
  </elmah>

これがコードです。

private void UpdateElmahErrorEmailSubject( string appPath )
{
    string machineName = System.Environment.MachineName;

    //System.Collections.IDictionary config = ( System.Collections.IDictionary ) ConfigurationManager.GetSection( "elmah" ); ;
    //System.Configuration.Configuration config2 = ( System.Configuration.Configuration ) ConfigurationManager.GetSection( "elmah/errorMail" );

    System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( appPath );
    if ( config == null )
    {
        return;
    }

    ConfigurationSectionGroup sectionGroup  = config.GetSectionGroup( "elmah" );
    ConfigurationSection section            = config.GetSection( "elmah/errorMail" );

    // i was not able to get directly to the subject, so I had to write it as xml
    string s = section.SectionInformation.GetRawXml();

    string search = "ServerBoxName:";

    //here is where i started to feel dirty, direct string parsing.
    int startIndex      = s.IndexOf( search );
    int endIndex        = s.IndexOf( "\"", startIndex );
    string toReplace    = s.Substring( startIndex, ( endIndex - startIndex ) );
    s                   = s.Replace( toReplace, search + " " + machineName );

    section.SectionInformation.SetRawXml( s );

    config.Save();
}

誰でも文字列の解析を回避できますか。私はそれをxmlとして取得しようとしましたが、それでも件名を解析する文字列になってしまいました。もっと良い方法はありますか?

ありがとう、

エリック-

4

3 に答える 3

3

Factory メソッドを使用して、実行時に HttpHandler を動的に作成します。ELMAH として必要な HTTPHandler のカスタム バリアントを作成します。これは、前回使用した HTTP ハンドラーのセットであることがわかっています。

この例を見てください:

http://www.informit.com/articles/article.aspx?p=25339&seqNum=5

于 2009-07-30T21:47:07.220 に答える
3

実行時に構成 XML を変更するのは、やり過ぎのように思えます。代わりに、すでに ELMAH に組み込まれているフックを使用します。たとえば、ELMAH はエラー メールを送信するときにイベントを発生させます。背景については、 Scott MitchellによるELMAH のエラー メールのカスタマイズブログ エントリを読むことをお勧めします。メールの準備が完了し、ELMAH がメールを送信する前に件名を操作するために、イベントのハンドラーを記述できます。また、次の例に示すように、件名の関連部分にパッチを適用します (文字列を検索し、インデックスを作成して部分を置き換えるよりも堅牢です)。MailingGlobal.asaxRegex

void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs args)
{
    args.Mail.Subject = 
        Regex.Replace(args.Mail.Subject, 
                      @"(?<=\bServerBoxName *: *)([_a-zA-Z0-9-]+)", 
                      Environment.MachineName);
}
于 2014-04-30T22:16:54.203 に答える
1

それを XML としてロードしてサブジェクトを取得し、次に String.Split を使用してサブジェクトを 2 回解析します。最初は "," で、次に結果の各文字列を ":" で解析します。次のような配列のリストを取得します。

最初の分割: Array 0 Application:EditStaff_MVC 1 Environment:Dev 2 ServerBoxName:AUST-DDEVMX90FF

2 番目 (内側) の分割: 配列 0 アプリケーション 1 EditStaff_MVC

2 番目の分割で、最初の値が ServerBoxName の場合、2 番目の値を自分の machineName で書き換えます。途中で文字列を再構築します。

于 2009-07-30T22:12:00.273 に答える