Log4Net的Config配置文件读取

本文介绍了利用System.Configuration命名空间中的IConfigurationSectionHandler接口,为ASP.NET应用配置文件编写自定义解析器。通过示例代码展示了如何使用XmlSerializerSectionHandler处理配置文件,还说明了如何将配置文件部分映射到该处理程序,以及XmlSerializer在初始化类型实例中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If you've ever seen the web.config file that ASP.NET uses, and thought, "Wow, I'm so glad they configure things that way, in this nice, easy-to-use XML file. Wish I could do that!" then you should check out the System.Configuration namespace. There's an interface called IConfigurationSectionHandler that lets you write your own parsers for your application configuration file, letting you put whatever you want in there.

I've written a bunch of these over the last few months, but today I wrote the last one I ever will. Here's the code for it:

using System;

using System.Configuration ;

using System.Xml ;

using System.Xml.Serialization ;

using System.Xml.XPath ;


namespace DevelopMentor.Candera.Utilities

{

  public class XmlSerializerSectionHandler :
                    
IConfigurationSectionHandler {

    public object Create(

                     object parent,

                     object configContext ,

                     System.Xml.XmlNode section) {

       XPathNavigator nav = section.CreateNavigator ();

      string typename = ( string ) nav.Evaluate ("string(@type)");

      Type t = Type.GetType ( typename );

       XmlSerializer ser = new XmlSerializer (t);

      return ser.Deserialize ( new XmlNodeReader (section));

    }

  }

}


It’s very, very simple. When you map a section of your configuration file to this handler, using an entry in your config file that looks like this:


< configuration >

  < configSections >

    < section name =" MyStuff "         t ype ="DevelopMentor.Candera.Utilities.XmlSerializerSectionHandler, Utilities"/>

  </ configSections >

</ configuration >


What happens is that any time you ask for the MyStuff section of the config file with code that looks like this:


ConfigurationSettings.GetConfig (" MyStuff ");


It will actually go look in the config file to figure out who knows how to deal with the “MyStuff” section. When it sees that it’s the XmlSerializerSectionHandler, it’ll create an instance of this object and call Create, passing it (via the section parameter) a reference to the relevant portion of the config file. In our case, it might look something like this:


< configuration >


   <!-- configSections element goes here -->  


  < MyStuff type =" SomeNamespace.MyStuff , CraigsConfig ">

    < Foo > 1.234 </ Foo >

    < Bar > A bunch of information </ Bar >

   </ MyStuff >


</ configuration >


What my handler does is to look for the “type” attribute on this bit of XML, in the form “classname, assemblyname”. In this case, it’s a class called MyStuff in a namespace SomeNamespace. It then – and here’s the good bit – uses the XmlSerializer to initialize an instance of this type from the XML in the configuration file. The XmlSerializer is a very cool piece of the .NET libraries, and you should check it out if you haven’t already. Basically, you write a type like this:



public class MyStuff

{

  private float foo ;

  private string bar;


  public float Foo

  {

    get { return foo ; }

    set { foo = value ; }

  }


  public string Bar

  {

     get { return bar; }

    set { bar = value ;

  }

}


And the XmlSerializer takes care of turning it into the XML above and vice versa. There are even a bunch of attributes you can use to control the process.


So given the section handler I wrote, any time I want to put some information in a config file, all I have to do is write a type like MyStuff, map a section to my XmlSerializerSectionWriter, write some XML into the config file with the values I’d like to capture and then ask the infrastructure to read it like this:


MyStuff ms = ( MyStuff ) ConfigurationSettings.GetConfig (" MyStuff ");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值