私は非常に多くのレベルで罪を犯しました。誰かがこの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として取得しようとしましたが、それでも件名を解析する文字列になってしまいました。もっと良い方法はありますか?
ありがとう、
エリック-