简体   繁体   中英

how to create log file to errors?

i want to create xml file for errors when database not access.when database not access then we create log in xml file.i want when user login to site if database not access then create log.if database is access then xml log file load and insert my table in my database.my code for login is :

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);

when database not access for Several times xml file is:

          <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>

error is "There is an error in XML document (9, 12)." in Deserialize.how to fix it?

add a parent class and include the List<BLLLog> as a property

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();

This page has some useful advises: Efficient Techniques for Modifying XML Files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM