[英]how to create log file to errors?
我想在無法訪問數據庫時為錯誤創建xml文件。當數據庫無法訪問時我們會在xml文件中創建日志。我想要在用戶登錄站點時訪問數據庫是否無法訪問則創建log.if數據庫是否可訪問則加載XML日志文件並將我的表插入數據庫中。我的登錄代碼為:
if (!DatabaseIsConnected())
{
BLLLog BLTemp = new BLLLog();
BLTemp.DateTime = DateTime.Now;
BLTemp.Event = "DB Not Access";
BLTemp.EventCode = (int)LogValues.DataBaseNotAccess;
BLTemp.ID = 0;
BLTemp.IpAddress = Core.GetIPAddress();
XmlSerializer serializer = new XmlSerializer(typeof(BLLLog));
StreamWriter xmlError = new StreamWriter(string.Concat(Server.MapPath("/"), "\\Sitemap\\XMLErrors.xml"),true);
serializer.Serialize(xmlError,BLTemp);
xmlError.Close();
throw new Exception("Db not access....");
}
//when loging
//write xmlError file to log table
StreamReader xmlError = new StreamReader(string.Concat(Server.MapPath("/"), "\\Sitemap\\XMLErrors.xml"));
XmlSerializer serializer = new XmlSerializer(typeof(BLLLog));
List<BLLLog> listLog = (List<BLLLog>)serializer.Deserialize(xmlError);
xmlError.Close();
HttpContext.Current.Response.Redirect(returnUrl);
當幾次XML文件無法訪問數據庫時:
<BLLLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>0</ID>
<DateTime>2015-10-15T12:00:22.0122383+03:30</DateTime>
<UserID>0</UserID>
<Event>DB Not Access</Event>
<EventCode>1000</EventCode>
<IpAddress>::1</IpAddress>
</BLLLog><?xml version="1.0" encoding="utf-8"?>
<BLLLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>0</ID>
<DateTime>2015-10-15T12:00:24.1353597+03:30</DateTime>
<UserID>0</UserID>
<Event>DB Not Access</Event>
<EventCode>1000</EventCode>
<IpAddress>::1</IpAddress>
</BLLLog><?xml version="1.0" encoding="utf-8"?>
<BLLLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>0</ID>
<DateTime>2015-10-15T12:00:25.8074554+03:30</DateTime>
<UserID>0</UserID>
<Event>DB Not Access</Event>
<EventCode>1000</EventCode>
<IpAddress>::1</IpAddress>
</BLLLog><?xml version="1.0" encoding="utf-8"?>
<BLLLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>0</ID>
<DateTime>2015-10-15T12:00:26.8995178+03:30</DateTime>
<UserID>0</UserID>
<Event>DB Not Access</Event>
<EventCode>1000</EventCode>
<IpAddress>::1</IpAddress>
</BLLLog>
錯誤為“ XML文檔(9,12)中存在錯誤。” 在Deserialize。如何修復它?
添加一個父類,並將List<BLLLog>
作為屬性包括在內
public class MyErrorLog
{
public List<BLLLog> Logs{get;set;}
}
var BLTemp = new MyErrorLog();
BLTemp.Logs = new List<BLLLog>();
var log = new BLLLog();
log.DateTime = DateTime.Now;
log.Event = "DB Not Access";
log.EventCode = (int)LogValues.DataBaseNotAccess;
log.ID = 0;
log.IpAddress = Core.GetIPAddress();
BLTemp.Logs.Add(log);
XmlSerializer serializer = new XmlSerializer(typeof(MyErrorLog));
StreamWriter xmlError = new StreamWriter(string.Concat(Server.MapPath("/"), "\\Sitemap\\XMLErrors.xml"),true);
serializer.Serialize(xmlError,BLTemp);
xmlError.Close();
StreamReader xmlError = new StreamReader(string.Concat(Server.MapPath("/"), "\\Sitemap\\XMLErrors.xml"));
XmlSerializer serializer = new XmlSerializer(typeof(MyErrorLog));
MyErrorLog listLog = (MyErrorLog)serializer.Deserialize(xmlError);
xmlError.Close();
該頁面有一些有用的建議: 修改XML文件的有效技術
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.