做底层项目经常会需要保存配置信息.为了方便运维,还是把配置文件单独分离出来配置比较好 ,一个项目一个单独的独立配置文件,简单,易维护,易移植.
全部都放在一个Web.config中.拥挤不堪,找起来麻烦,看着都烦.写说明文档也麻烦的要死..还是独立配置比较清爽..
下面是读取单独配置文件的类
public class XMLConfigRead
{
public string ConfigPath;
public XMLConfigRead(string fileName)
{
//HttpContext.Current.Request.PhysicalApplicationPath 取得config文件路径
string dllPath = string.Empty;
try
{
//dllPath = HttpContext.Current.Request.PhysicalApplicationPath;
dllPath = HttpRuntime.AppDomainAppPath;//Web环境下 这个最稳定.应该用这个.
}
catch (Exception ex)
{
dllPath = Path.GetDirectoryName(new Uri(this.GetType().Assembly.CodeBase).AbsolutePath); //本地应用程序的情况下.应该用这个
}
if (File.Exists(fileName))
{
ConfigPath = fileName;
}
else if (File.Exists(Path.Combine(dllPath, fileName)))
{
ConfigPath = Path.Combine(dllPath, fileName);
}
else
{
throw new Exception("配置文件不存在,请检查");
}
}
public string ReadNodeValue (string nodename)
{
XmlDocument doc = new XmlDocument();
doc.Load(ConfigPath);
XmlNode node = doc.SelectSingleNode(nodename);
if (node.Value == null)
{
if (node.Attributes["value"] == null)
{
return "";
}else{
return node.Attributes["value"].Value;
}
}else{
return node.Value;
}
}
}
把 WebSiteErrorLog.config 配置文件,直接放在网站根目录下,
读取方法是
XMLConfigRead ConfigRead = new XMLConfigRead(“WebSiteErrorLog.config”);
当然也可以把配置文件都放到config下面 读取方法得改成
XMLConfigRead ConfigRead = new XMLConfigRead(“config/WebSiteErrorLog.config”);
<?xml version="1.0" encoding="utf-8" ?>
<ErrorLog>
<DB_DataSource value="10.90.0.0*"/>
<DB_InitialCatalog value="Log"/>
<DB_IntegratedSecurity value="false"/>
<DB_MaxPoolSize value="10000"/>
<DB_ConnectTimeout value="1"/>
<DB_UserID value="sa"/>
<DB_Password value="999"/>
<WebSiteId value="123456789"/>
</ErrorLog>
//使用方法, 读取WebSiteErrorLog.config 配置文件 中的配置
见下面代码
public class StoreErrorToDB
{
XMLConfigRead ConfigRead = new XMLConfigRead("WebSiteErrorLog.config");
static SqlConnectionStringBuilder connStr ;
static string WebSiteId;
public StoreErrorToDB()
{
if (connStr == null)
{
connStr = new SqlConnectionStringBuilder();
connStr.DataSource = ConfigRead.ReadNodeValue("ErrorLog/DB_DataSource");
connStr.InitialCatalog = ConfigRead.ReadNodeValue("ErrorLog/DB_InitialCatalog");
connStr.IntegratedSecurity = Convert.ToBoolean(ConfigRead.ReadNodeValue("ErrorLog/DB_IntegratedSecurity"));
connStr.UserID = ConfigRead.ReadNodeValue("ErrorLog/DB_UserID");
connStr.Password = ConfigRead.ReadNodeValue("ErrorLog/DB_Password");
//设置最大连接池
connStr.MaxPoolSize = Convert.ToInt32(ConfigRead.ReadNodeValue("ErrorLog/DB_MaxPoolSize"));
//设置超时时间为1秒
connStr.ConnectTimeout = Convert.ToInt32(ConfigRead.ReadNodeValue("ErrorLog/DB_ConnectTimeout"));
WebSiteId = ConfigRead.ReadNodeValue("ErrorLog/WebSiteId");
}
}
public void SaveError(Error error)
{
SqlConnection conn = new SqlConnection(connStr.ConnectionString);
conn.Open();
string sql = @" INSERT INTO Error
(Id,WebSiteId,RequestQueryString,RequestUserAgent,RequestUserHostAddress,RequestUrl,RequestUrlReferrer,RequestRequestType,RequestHttpMethod,RequestContentEncoding,InnerException_Message,InnerException_Source,InnerException_StackTrace,InnerException_TargetSite,Message,Source,StackTrace,TargetSite,RequestCookie,RequestHeaders,RequestForms)
VALUES
(@Id,@WebSiteId,@RequestQueryString,@RequestUserAgent,@RequestUserHostAddress,@RequestUrl,@RequestUrlReferrer,@RequestRequestType,@RequestHttpMethod,@RequestContentEncoding,@InnerException_Message,@InnerException_Source,@InnerException_StackTrace,@InnerException_TargetSite,@Message,@Source,@StackTrace,@TargetSite,@RequestCookie,@RequestHeaders,@RequestForms)";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.Add("@RequestQueryString",SqlDbType.VarChar,4000);
cmd.Parameters.Add("@RequestUserAgent",SqlDbType.VarChar,100);
cmd.Parameters.Add("@RequestUserHostAddress",SqlDbType.VarChar,500);
cmd.Parameters.Add("@RequestUrl",SqlDbType.VarChar,500);
cmd.Parameters.Add("@RequestUrlReferrer",SqlDbType.VarChar,500);
cmd.Parameters.Add("@RequestRequestType",SqlDbType.VarChar,10);
cmd.Parameters.Add("@RequestHttpMethod",SqlDbType.VarChar,10);
cmd.Parameters.Add("@RequestContentEncoding",SqlDbType.VarChar,10);
cmd.Parameters.Add("@InnerException_Message",SqlDbType.VarChar,500);
cmd.Parameters.Add("@InnerException_Source",SqlDbType.VarChar,500);
cmd.Parameters.Add("@InnerException_StackTrace",SqlDbType.VarChar,600);
cmd.Parameters.Add("@InnerException_TargetSite",SqlDbType.VarChar,600);
cmd.Parameters.Add("@Message",SqlDbType.VarChar,500);
cmd.Parameters.Add("@Source",SqlDbType.VarChar,500);
cmd.Parameters.Add("@StackTrace",SqlDbType.VarChar,600);
cmd.Parameters.Add("@TargetSite", SqlDbType.VarChar, 600);
cmd.Parameters.Add("@WebSiteId", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@Id", SqlDbType.UniqueIdentifier, 60);
cmd.Parameters.Add("@RequestCookie", SqlDbType.VarChar, 4000);
cmd.Parameters.Add("@RequestHeaders", SqlDbType.VarChar, 4000);
cmd.Parameters.Add("@RequestForms", SqlDbType.VarChar, 4000);
cmd.Parameters[0].Value = error.RequestQueryString;
cmd.Parameters[1].Value = error.RequestUserAgent;
cmd.Parameters[2].Value = error.RequestUserHostAddress;
cmd.Parameters[3].Value = error.RequestUrl;
cmd.Parameters[4].Value = error.RequestUrlReferrer;
cmd.Parameters[5].Value = error.RequestRequestType;
cmd.Parameters[6].Value = error.RequestHttpMethod;
cmd.Parameters[7].Value = error.RequestContentEncoding;
cmd.Parameters[8].Value = error.InnerException_Message;
cmd.Parameters[9].Value = error.InnerException_Source;
cmd.Parameters[10].Value = error.InnerException_StackTrace;
cmd.Parameters[11].Value = error.InnerException_TargetSite;
cmd.Parameters[12].Value = error.Message;
cmd.Parameters[13].Value = error.Source;
cmd.Parameters[14].Value = error.StackTrace;
cmd.Parameters[15].Value = error.TargetSite;
cmd.Parameters[16].Value = WebSiteId;
cmd.Parameters[17].Value = error.Id;
cmd.Parameters[18].Value = error.RequestCookie;
cmd.Parameters[19].Value = error.RequestHeaders;
cmd.Parameters[20].Value = error.RequestForms;
cmd.ExecuteNonQuery();
conn.Close();
}
}