Log4Net的Config配置文件读取
来源:互联网 发布:淘宝浦发信用卡电影票 编辑:程序博客网 时间:2024/05/14 09:18
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 ");
- Log4Net的Config配置文件读取
- 配置文件的读取(.config)
- Log4net.config配置文件
- 读取log4net配置文件方法
- 配置文件读取的一种方法config.properties
- c#读取配置文件 程序的App.config
- C++读取配置文件的封装类config
- web.config配置文件读取
- 动态读取配置文件 config
- ConfigParser 读取 config 配置文件
- log4net的配置文件
- log4net 的配置文件
- ASP.Net2.0使用Log4Net(二)使用单独的配置文件,不放在web.config
- Silverlight读取Web.config配置文件
- [XML]读取Config配置文件值
- java读取config.properties配置文件
- thinkphp 读取config.php配置文件
- Silverlight读取web.config配置文件
- 各种各样的面试问题
- 学编程,决不可心浮气燥。任何实践都是……
- C语言:陷阱和缺陷
- 把论坛升级到3.03了
- 帮你免于失业的十大软件技术
- Log4Net的Config配置文件读取
- C程序开发规范
- 伪造IP包,禁止TCP连接
- 在.net中轻松掌握Windows窗体间的数据交互
- 回退的相关知识
- VB.NET/ASP.NET编码规范(ZT)
- WAVE文件格式剖析
- javascript自动的校验函数(二)-email校验
- About Common Method's Collection