Exemplo n.º 1
0
        /// <summary>
        /// 写入活动日志
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        private static void WriteLog(string path, string content)
        {
            string          _fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".xml"; //文件名
            string          _filePath = path + "//logs//" + _fileName;                //文件路径
            string          _version  = null;
            OperatingSystem os        = Environment.OSVersion;
            Version         vs        = Environment.Version;

            try
            {
                try
                {
                    _version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
                }
                catch
                {
                }
                if (!File.Exists(_filePath))                               //第一次创建
                {
                    EnowitXmlNode root     = new EnowitXmlNode("Runtime"); //根节点
                    EnowitXmlNode logsNode = InitLogNode(content);
                    root.ChildNode.Add(logsNode);
                    EnowitXml.CreateXmlFile(_filePath, Encoding.UTF8, root);//创建XMl文件
                }
                else
                {
                    //string version = "";
                    try
                    {
                        //version = EnowitXml.GetAttributeValue(_filePath, "//Runtime//logs//log", "Verson");
                        bool Flag = EnowitXml.ExistNode(_filePath, "//Runtime//logs", "Verson", _version);
                        if (Flag)//版本相同怎继续在下面添加日志节点
                        {
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(_filePath);

                            XmlElement xmlContent = xmlDoc.CreateElement("content");
                            xmlContent.SetAttribute("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
                            xmlContent.InnerText = content;

                            string xPath = string.Format("//Runtime//logs//log[@Verson='{0}']", _version);
                            EnowitXml.AppendNode(_filePath, xPath, xmlContent);
                        }
                        else//版本不同时再次创建log节点
                        {
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(_filePath);
                            XmlElement xmlElement = xmlDoc.CreateElement("log");
                            xmlElement.SetAttribute("Verson", System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString());
                            xmlElement.SetAttribute("Assembly", System.Reflection.Assembly.GetEntryAssembly().GetName().FullName.ToString());

                            XmlElement xmlContent = xmlDoc.CreateElement("content");
                            xmlContent.SetAttribute("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
                            xmlContent.InnerText = content;
                            xmlElement.AppendChild(xmlContent);

                            EnowitXml.AppendNode(_filePath, "//Runtime//logs", xmlElement);
                        }
                    }
                    catch (NullReferenceException) {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(_filePath);
                        XmlElement xmlLogs = xmlDoc.CreateElement("logs");

                        XmlElement xmlElement = xmlDoc.CreateElement("log");
                        xmlElement.SetAttribute("Verson", System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString());
                        xmlElement.SetAttribute("Assembly", System.Reflection.Assembly.GetEntryAssembly().GetName().Name.ToString());


                        XmlElement xmlContent = xmlDoc.CreateElement("content");
                        xmlContent.SetAttribute("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
                        xmlContent.InnerText = content;
                        xmlElement.AppendChild(xmlContent);

                        xmlLogs.AppendChild(xmlElement);
                        EnowitXml.AppendNode(_filePath, "//Runtime", xmlLogs);
                    }
                }
            }
            catch
            {
            }
        }
Exemplo n.º 2
0
        private static void WriteErrorLog(Exception ex, string path)
        {
            string          _fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".xml"; //文件名
            string          _filePath = path + "//logs//" + _fileName;                //文件路径
            OperatingSystem os        = Environment.OSVersion;
            Version         vs        = Environment.Version;

            try
            {
                if (!File.Exists(_filePath))                                     //第一次创建Exception日志
                {
                    EnowitXmlNode root           = new EnowitXmlNode("Runtime"); //根节点
                    EnowitXmlNode expectionsNode = InitErrorNode(ex, os, vs);
                    root.ChildNode.Add(expectionsNode);

                    EnowitXml.CreateXmlFile(_filePath, Encoding.UTF8, root);//创建XMl文件
                }
                else
                {
                    //Assembly assembly = Assembly.GetExecutingAssembly().GetName().Version;
                    //string _version = "";
                    string version = Assembly.GetExecutingAssembly().GetName().Version.Major.ToString();
                    //string version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
                    try
                    {
                        //_version = EnowitXml.GetAttributeValue(_filePath, "//Runtime//exceptions//error", "Verson");
                        bool Flag = EnowitXml.ExistNode(_filePath, "//Runtime//exceptions", "Verson", System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString());
                        if (Flag)//有相同版本时继续在几点下添加内容节点
                        {
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(_filePath);
                            string     xPath       = string.Format("//Runtime//exceptions//error[@Verson='{0}']", version);
                            XmlElement xmlExpecion = InitErrorElement(xmlDoc, os, vs, ex);
                            EnowitXml.AppendNode(_filePath, xPath, xmlExpecion);
                        }
                        else//没有添加令一版本的节点Exception
                        {
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(_filePath);
                            //error节点
                            XmlElement xmlError = xmlDoc.CreateElement("error");
                            xmlError.SetAttribute("Verson", version);
                            xmlError.SetAttribute("Assembly", ex.Source);
                            XmlElement xmlExpecion = InitErrorElement(xmlDoc, os, vs, ex);
                            xmlError.AppendChild(xmlExpecion);
                            EnowitXml.AppendNode(_filePath, "//Runtime//exceptions", xmlError);
                        }
                    }
                    catch (NullReferenceException) {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(_filePath);
                        XmlElement xmlExceptions = xmlDoc.CreateElement("exceptions");

                        //error节点
                        XmlElement xmlError = xmlDoc.CreateElement("error");
                        xmlError.SetAttribute("Verson", version);
                        xmlError.SetAttribute("Assembly", ex.Source);
                        XmlElement xmlExpecion = InitErrorElement(xmlDoc, os, vs, ex);
                        xmlError.AppendChild(xmlExpecion);
                        xmlExceptions.AppendChild(xmlError);
                        EnowitXml.AppendNode(_filePath, "//Runtime", xmlExceptions);
                    }
                }
            }
            catch
            {
            }
        }