Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are enums represented in a web.config file?

I thought this questions was trivial and that Enums were represented simply with an integer but I was surprised that it wasn't the case ! Here's what I have :

I have a custom config file that contains the following TraceEventType Property

  [ConfigurationProperty("Severity")]
  public TraceEventType Severity
  {
     get { return (TraceEventType)this["Severity"]; }
     set { this["Severity"] = value; }
  }

In my config file, I represented this property with the following value :

  ...
  <FileLog  Filename="Test" 
            Severity="1" />

The result : I got this :

System.Configuration.ConfigurationErrorsException: The value of the property 'Severity' cannot be parsed. The error is: The enumeration value must be one of the following: Critical, Error, Warning, Information, Verbose, Start, Stop, Suspend, Resume, Transfer.

like image 788
Mike Verrier Avatar asked Oct 22 '22 05:10

Mike Verrier


1 Answers

You should be able to just use the names of your enum values in the config file, just like the error message states. For example:

<FileLog Filename="Test" Severity="Verbose" />

The System.Configuration classes will take care of parsing the enum for you.

like image 92
rsbarro Avatar answered Oct 31 '22 12:10

rsbarro