简体   繁体   English

如何创建日志文件出错?

[英]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 : 我想在无法访问数据库时为错误创建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);

when database not access for Several times xml file is: 当几次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>

error is "There is an error in XML document (9, 12)." 错误为“ XML文档(9,12)中存在错误。” in Deserialize.how to fix it? 在Deserialize。如何修复它?

add a parent class and include the List<BLLLog> as a property 添加一个父类,并将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();

This page has some useful advises: Efficient Techniques for Modifying XML Files 该页面有一些有用的建议: 修改XML文件的有效技术

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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