1

I'm not quite sure what I am doing wrong. I have been following the article here: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx and from my understanding I have this setup correctly. Here is my code:

public class LoggerConfiguration : ConfigurationSection
{
    [ConfigurationProperty("level")]
    public LoggerConfigurationLevel Level
    {
        get { return (LoggerConfigurationLevel) this["level"]; }
        set { this["level"] = value; }
    }
}

public class LoggerConfigurationLevel : ConfigurationElement
{
    [ConfigurationProperty("logLevel", IsRequired = true)]
    [StringValidator(MaxLength = 5, MinLength = 4, InvalidCharacters = "!@#$%^&*()_+12345567890-=`~[]\\;',./{}|:\"<>?HJKMPQSVXYZ")]
    public string LogLevel
    {
        get { return (string)this["logLevel"]; }
        set { this["logLevel"] = value; }
    }

    [ConfigurationProperty("fileExtension", IsRequired = true)]
    [StringValidator(InvalidCharacters = "!@#$%^&*()_+12345567890-=`~[]\\;',./{}|:\"<>?")]
    public string FileExtension
    {
        get { return (string)this["fileExtension"]; }
        set { this["fileExtension"] = value; }
    }

    [ConfigurationProperty("logDirectory", DefaultValue = "", IsRequired = false)]
    public string LogDirectory
    {
        get { return (string)this["logDirectory"]; }
        set { this["logDirectory"] = value; }
    }

    [ConfigurationProperty("messageLayout", DefaultValue = "${message}${exception:format=tostring}", IsRequired = false)]
    public string MessageLayout
    {
        get { return (string)this["messageLayout"]; }
        set { this["messageLayout"] = value; }
    }
}

Then my web.config looks like this:

<configuration>
  <configSections>
    <sectionGroup name="loggerConfigurationGroup">
        <section  name="logggerConfiguration" 
                  type="Website.Models.LoggerConfiguration"  
                  allowLocation="true" 
                  allowDefinition="Everywhere" />
    </sectionGroup>
  </configSections>
  <loggerConfigurationGroup>
    <loggerConfiguration>
      <loggerConfigurationLevel>
        <level LogLevel="Debug" FileExtension="log" LogDirectory="C:\Logs" MessageLayout="${message}${exception:format=tostring}" />
      </loggerConfigurationLevel>
    </loggerConfiguration>
  </loggerConfigurationGroup>
</configuration>

The error that I get is my website loads a blank page and in the console (in developer tools on the browser) it says 500 error. I am sure it is because I have misconfigured here something in the web.config, I just can't figure out what.

EDIT:

So I decided to go back to step 1 and try to implement the example shown in the link I gave above. Interesting part is that that did not work for me either.

I copied the code and web config and when I go to print out the value in the Font Name, I get Aria, vs TimesNewRoman that is listed in the web.config.

Is that article simply out of date then? Are there other ways of accomplishing this?

EDIT2:

So I have simplified this code, here is the code that I have now:

public sealed class MailCenterConfiguration : ConfigurationSection
{
    [ConfigurationProperty("userDiskSpace", IsRequired = true)]
    [IntegerValidator(MinValue = 0, MaxValue = 1000000)]
    public int UserDiskSpace
    {
        get { return (int)base["userDiskSpace"]; }
        set { base["userDiskSpace"] = value; }
    }
}

My Web.config:

<configuration>
    <configSections>
        <section name="mailCenter" type="Website.Models.MailCenterConfiguration" requirePermission="false"/>
    </configSections>
    <mailCenter userDiskSpace="25000"></mailCenter>
</configuration>

Now the page loads but the value it spits out is 0 as oppose to 25000.

The above codes is based on the SO answer here: How do I define custom web.config sections with potential child elements and attributes for the properties?

Community
  • 1
  • 1
Bagzli
  • 6,254
  • 17
  • 80
  • 163

0 Answers0