Reputation: 7715
Background
I want to insert the following into my web.config
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
</sectionGroup>
(no prizes for guessing what I am trying to achieve!) but I am getting thoroughly confused. The docs on MSDN suggest that I need to create a subclass of ConfurationSection if I want to add one to a group. I wrote myself a little windows application to help me figure it out but I did not get very far! Here is the pertinent code - which tries to add the just the "security" section.
private void AddElmahSectionGroup()
{
string exePath = Path.Combine(Environment.CurrentDirectory, "NameOfExe.exe");
Configuration configuration = ConfigurationManager.OpenExeConfiguration(exePath);
ConfigurationSectionGroup elmahGroup = configuration.GetSectionGroup(elmahSectionGroupName);
if (elmahGroup != null)
{
Console.WriteLine("sectionGroup with name {0} already in web.config", elmahSectionGroupName);
return;
}
elmahGroup = new ConfigurationSectionGroup();
configuration.SectionGroups.Add(elmahSectionGroupName, elmahGroup);
var securitySection = new Section { Name = "security", RequirePermission = false, Type = "Elmah.SecuritySectionHandler, Elmah" };
elmahGroup.Sections.Add("security", securitySection);
configuration.Save();
}
public class Section : ConfigurationSection
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name { get { return (String)this["name"]; } set { this["name"] = value; } }
[ConfigurationProperty("requirePermission", IsRequired = true)]
public bool RequirePermission { get { return (bool)this["requirePermission"]; } set { this["requirePermission"] = value; } }
[ConfigurationProperty("type", IsRequired = true)]
public string Type { get { return (string)this["type"]; } set { this["type"] = value; } }
}
And here is the resultant configuration file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="elmah" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
<section name="security" type="ConfigEditing.Form1+ElmahLogic+Section, ConfigEditing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</sectionGroup>
</configSections>
<elmah>
<security name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
</elmah>
</configuration>
Which completely twisted my melon: -
type
of the section
element is the type of my class (derived from ConfigurationSection) I am not really surprised by these findings because I really don't understand the API but I just have not found any decent docs about what I want to do. Can anyone here help - even if it just pointing me at an MSDN example that is an actual full working example.
Upvotes: 3
Views: 8584
Reputation: 2106
A simple example that will add a section like the following to app.config:
//<configSections>
// <sectionGroup name="elmah" type="Overflow.CustomConfigurationSectionGroup, Overflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
// </sectionGroup>
//</configSections>
namespace Overflow
{
public class CustomSecuritySection : ConfigurationSection
{
}
public class CustomConfigurationSectionGroup : ConfigurationSectionGroup
{
public CustomConfigurationSectionGroup()
{
Security = new CustomSecuritySection();
}
[ConfigurationProperty("security")]
public CustomSecuritySection Security { get; private set; }
}
class Program
{
static void Main(string[] args)
{
var config = ConfigurationManager.OpenExeConfiguration(Path.Combine(Application.StartupPath, Application.ProductName + ".exe"));
config.SectionGroups.Add("elmah", new CustomConfigurationSectionGroup());
config.Save(ConfigurationSaveMode.Modified);
}
}
}
Upvotes: 2