title: Create a strongly typed log4net level from a string
body: <p>I noticed <a href="http://logging.apache.org/log4net/" rel="noreferrer">log4net</a> has a strongly typed <a href="https://logging.apache.org/log4net/log4net-1.2.11/release/sdk/log4net.Core.Level.html" rel="noreferrer">Level class</a>. Does anyone know if there's a way to create a Level object by parsing a log level string?</p>
  
  <p>For example:</p>
  
  <p>I want to parse the error level string "ERROR" into the equivalent log4net.Core.Level.Error object, and likewise for other logging levels.</p>
  
  <p>I wrote my own (naive) method to do this (see below), but I was hoping there was something internal to log4net that would do this for me.</p>
  
  <p>Thanks!</p>
  
  <pre><code>    public static Level ParseLevel(string level)
      {
          switch (level.ToUpperInvariant())
          {
              case "ALERT":
                  return log4net.Core.Level.Alert;
              case "ALL":
                  return log4net.Core.Level.All;
              case "CRITICAL":
                  return log4net.Core.Level.Critical;
              case "DEBUG":
                  return log4net.Core.Level.Debug;
              case "EMERGENCY":
                  return log4net.Core.Level.Emergency;
              case "ERROR":
                  return log4net.Core.Level.Error;
              case "FATAL":
                  return log4net.Core.Level.Fatal;
              case "FINE":
                  return log4net.Core.Level.Fine;
              case "FINER":
                  return log4net.Core.Level.Finer;
              case "FINEST":
                  return log4net.Core.Level.Finest;
              case "INFO":
                  return log4net.Core.Level.Info;
              case "NOTICE":
                  return log4net.Core.Level.Notice;
              case "OFF":
                  return log4net.Core.Level.Off;
              case "SEVERE":
                  return log4net.Core.Level.Severe;
              case "TRACE":
                  return log4net.Core.Level.Trace;
              case "VERBOSE":
                  return log4net.Core.Level.Verbose;
              case "WARN":
                  return log4net.Core.Level.Warn;
              default:
                  throw new Exception("Invalid logging level specified");
          }
      }
  </code></pre>
  
  <p>Edit: Based on Dr. Wily's Apprentice's recommendation, here's what I came up with in the end using the <a href="https://logging.apache.org/log4net/log4net-1.2.11/release/sdk/log4net.Core.LevelMapMembers.html" rel="noreferrer">LevelMap</a>:</p>
  
  <pre><code>    using System;
      using System.Linq;
  
      public static Level ParseLevel(string level)
      {
          var loggerRepository = LoggerManager.GetAllRepositories().FirstOrDefault();
  
          if (loggerRepository == null)
          {
              throw new Exception("No logging repositories defined");
          }
  
          var stronglyTypedLevel = loggerRepository.LevelMap[level];
  
          if (stronglyTypedLevel == null)
          {
              throw new Exception("Invalid logging level specified");
          }
  
          return stronglyTypedLevel;
      }
  </code></pre>