如何创建日志文件错误?

时间:2015-10-15 08:36:15

标签: c# asp.net xml

我想在数据库无法访问时创建错误的xml文件。当数据库无法访问时我们创建登录xml file.i想要当用户登录到站点时如果数据库无法访问则创建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.how中解决它?

1 个答案:

答案 0 :(得分:0)

添加父类并将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();

此页面提供了一些有用的建议:Efficient Techniques for Modifying XML Files