C# 如何创建日志文件以避免错误?
当数据库不可访问时,我想为错误创建xml文件。当数据库不可访问时,我们在xml文件中创建日志。当用户登录到站点时,如果数据库不可访问,我想创建日志。如果数据库是可访问的,则xml日志文件加载并在我的数据库中插入我的表。我的登录代码是:C# 如何创建日志文件以避免错误?,c#,asp.net,xml,C#,Asp.net,Xml,当数据库不可访问时,我想为错误创建xml文件。当数据库不可访问时,我们在xml文件中创建日志。当用户登录到站点时,如果数据库不可访问,我想创建日志。如果数据库是可访问的,则xml日志文件加载并在我的数据库中插入我的表。我的登录代码是: if (!DatabaseIsConnected()) { BLLLog BLTemp = new BLLLog(); BLTemp.DateTime = DateTime.N
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中存在错误。在反序列化中。如何修复它?添加父类并将列表作为属性包含
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文件,则缺少根节点,因此这不是有效的xml。错误:生成xml文档时出错