I have sinned on so many levels. I'm hoping someone can tell me a better way to re-write this c#.
I was given a task to modify a section of the web.config at runtime to remove a piece of the subject for an elmah error email and inset the box name.
the reason is we can't trust our cm folks to get these right consistantly and so we waste time debugging errors on the wrong box.
so with that uglyness ahead of me i started writing...
here is the section in the web.config I'm trying to modify
<elmah>
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ELMAH" />
<errorMail from="..." to="..."
subject="Application: EditStaff_MVC, Environment:Dev, ServerBoxName: AUST-DDEVMX90FF"
async="true" />
</elmah>
here is the code.
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();
}
can anyone get around the string parsing. I tried getting to it as xml, but still wound up string parsing the subject. Is there a better way?
Thanks,
Eric-