私は非常に多くのレベルで罪を犯しました。誰かがこのC#を書き直すより良い方法を教えてくれることを願っています。私は罪を犯しました。実行時にWeb.configを書き直しました... elmahのために
実行時にweb.configのセクションを修正して、elmahエラーの電子メールの件名を削除し、ボックス名を挿入するタスクが与えられました。
なぜなら私たちのcmの人々はこれらの権利を一貫して得ることができないからです。 そして、間違ったボックスにエラーをデバッグするのは時間を無駄にします。私が書き始めた先に私のことuglynessとそう
...
ここでは、私がここに
<elmah>
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ELMAH" />
<errorMail from="..." to="..."
subject="Application: EditStaff_MVC, Environment:Dev, ServerBoxName: AUST-DDEVMX90FF"
async="true" />
</elmah>
を変更しようとしているweb.configファイル内のセクションでは、コードです。
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として取得しようとしましたが、まだ文字列を解析して文字列を解析しました。 良い方法がありますか?
おかげで、
Eric-
あなたがしている値を格納することができますサーバーのmachine.config内のサーバーに固有のものですか?あなたはそれを正しく得るためにあなたのcmの人たちを依然として信頼しなければなりませんが、一度それが正しいとすれば、それは正しく留まるべきです。 – BStruthers
悲しいかな、全体的にはCmの問題を避けるために、これはそれらを減らしますが、それらを削除することはありません。 –
私はコードの美しいフォーマットのためだけにあなたをupvoteする必要があります。非常に素晴らしい。整列された等価は真の方法です... –