近来使用微软的ExceptionManagement,它要求在config设置中指定一个有写权限的文件夹以写入错误日志,在WinForm程序中,这并不难。因为一个WinForm程序一般对整个磁盘都有写的权限(不太确定)。所以可以指定一个不在程序目录下的默认文件夹(例如:C:/App_Error/)作为错误记录文件夹。但一个Web应用程序不能做到这一点,一般来说Web应用程序要么没有任何写权限,要么只能读写程序目录及子目录。所以这就要求一个ASP.NET应用程序能够自动识别根目录物理路径的改变,并作为参数值用程序写入到Web.config中。下面的代码就演示了如何读写XML,以达到此目的。
要想实现在程序中自动更改Web.config设置参数,有两种比较简单的方法:一是在安装时执行更改Web.cofig设置的代码;二是在Application_Start事件中执行更改Web.cofig设置的代码。采用前者时只能实现一次动态修改,不能保证在安装后程序根目录的物理路径改变时还能正常运行;而采用后者则在每次程序启动时,都会自动执行这段代码,增加了开销。具体采用哪种方法,应视具体程序而定。下面的代码仅仅以动态设置微软的ExceptionManagement配置为例演示在如何在Application_Start事件中执行更改Web.cofig设置,时间关系,代码中没有再添加注释,但对于需要的人来说,程序并不难读。
1
protected void Application_Start(Object sender, EventArgs e)
2
{
3
System.Xml.XmlDocument oXmlDoc = new System.Xml.XmlDocument();
4
oXmlDoc.Load(Server.MapPath("~/web.config"));
5
6
-= Error handling =-#region -= Error handling =-
7
System.Xml.XmlNode curNode = oXmlDoc.SelectSingleNode(@"/configuration");
8
if( null != curNode)
9
{
10
-= Exception Management =-#region -= Exception Management =-
11
System.Xml.XmlAttribute _mode = oXmlDoc.CreateAttribute("mode");
12
_mode.InnerText = "on";
13
14
-= Pulisher Properties =-#region -= Pulisher Properties =-
15
System.Xml.XmlAttribute _assembly = oXmlDoc.CreateAttribute("assembly");
16
_assembly.InnerText = "Microsoft.ApplicationBlocks.ExceptionManagement";
17
18
System.Xml.XmlAttribute _type2 = oXmlDoc.CreateAttribute("type");
19
_type2.InnerText = "Microsoft.ApplicationBlocks.ExceptionManagement.DefaultPublisher";
20
21
System.Xml.XmlAttribute _applicationname = oXmlDoc.CreateAttribute("applicationname");
22
string appName = System.Reflection.Assembly.GetAssembly(typeof(Default)).FullName;
23
if(null != appName && appName.IndexOf(',') > 0)
24
{
25
_applicationname.InnerText = appName.Substring(0,appName.IndexOf(','));
26
}
27
else
28
{
29
_applicationname.InnerText = "Unknown";
30
}
31
32
33
// System.Xml.XmlAttribute _exceptionFormat = oXmlDoc.CreateAttribute("exceptionFormat");
34
// _exceptionFormat.InnerText = "xml";
35
36
System.Xml.XmlAttribute _filePath = oXmlDoc.CreateAttribute("filePath");
37
// TODO:修改这里的bin为其他自定义的错误文件夹
38
_filePath.InnerText = Server.MapPath("~/bin/");
39
40
System.Xml.XmlAttribute _daily = oXmlDoc.CreateAttribute("daily");
41
_daily.InnerText = "true";
42
43
System.Xml.XmlNode _publisher = oXmlDoc.CreateElement("publisher");
44
45
_publisher.Attributes.Append(_mode);
46
_publisher.Attributes.Append(_assembly);
47
_publisher.Attributes.Append(_type2);
48
// _publisher.Attributes.Append(_exceptionFormat);
49
_publisher.Attributes.Append(_applicationname);
50
_publisher.Attributes.Append(_filePath);
51
_publisher.Attributes.Append(_daily);
52
#endregion
53
54
System.Xml.XmlNode _exceptionManagement = oXmlDoc.CreateElement("exceptionManagement");
55
_exceptionManagement.AppendChild(_publisher);
56
_exceptionManagement.Attributes.Append(_mode);
57
58
System.Xml.XmlNode exceptionManagement =
59
oXmlDoc.SelectSingleNode(@"/configuration/exceptionManagement");
60
if(null == exceptionManagement)
61
{
62
curNode.InsertBefore(_exceptionManagement,curNode.ChildNodes[0]);
63
}
64
else
65
{
66
curNode.ReplaceChild(_exceptionManagement,exceptionManagement);
67
}
68
#endregion
69
70
-= ConfigSections =-#region -= ConfigSections =-
71
System.Xml.XmlAttribute _name = oXmlDoc.CreateAttribute("name");
72
_name.InnerText = "exceptionManagement";
73
74
System.Xml.XmlAttribute _type = oXmlDoc.CreateAttribute("type");
75
_type.InnerText = "Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManagerSectionHandler,/r/nMicrosoft.ApplicationBlocks.ExceptionManagement";
76
77
System.Xml.XmlNode _section = oXmlDoc.CreateElement("section");
78
_section.Attributes.Append(_name);
79
_section.Attributes.Append(_type);
80
81
System.Xml.XmlNode _configSections = oXmlDoc.CreateElement("configSections");
82
_configSections.AppendChild(_section);
83
84
System.Xml.XmlNode configSections =
85
oXmlDoc.SelectSingleNode(@"/configuration/configSections");
86
if(null == configSections)
87
{
88
curNode.InsertBefore(_configSections,curNode.ChildNodes[0]);
89
}
90
else
91
{
92
curNode.ReplaceChild(_configSections,configSections);
93
}
94
95
#endregion
96
97
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(
98
Server.MapPath("~/web.config"),System.Text.Encoding.UTF8);
99
writer.Formatting = System.Xml.Formatting.Indented;
100
oXmlDoc.Save(writer);
101
writer.Close();
102
}
103
#endregion
104
}