private Exception GetExceptionToBeLogged(Exception ex) { ClientException cex = ex as ClientException; SqlException sqlEx = ex as SqlException; RawClientException rcex = ex as RawClientException; if (sqlEx != null && sqlEx.Number == -2) { cex = new ClientException(ClientExceptionId.SqlTimeout); } if (cex != null) { if (ConfigurationMapper.Instance.LogHandledExceptions) { return(ex); } else if (cex.InnerException != null) { return(cex.InnerException); } } else if (rcex != null && ConfigurationMapper.Instance.LogHandledExceptions) { return(ex); } //if its an unhandled exception return(ex); }
public XDocument CreateExceptionXml(Exception ex) { //load exceptions templates XDocument exceptionsTemplates; Assembly kernelAssembly = Assembly.GetAssembly(typeof(Kernel.Exceptions.ClientException)); StreamReader exReader = new StreamReader(kernelAssembly.GetManifestResourceStream("Makolab.Fractus.Kernel.Templates.Exceptions.xml")); exceptionsTemplates = XDocument.Parse(exReader.ReadToEnd()); exReader.Dispose(); // ClientException cex = ex as ClientException; XDocument exceptionXml = XDocument.Parse("<exception/>"); SqlException sqlEx = ex as SqlException; RawClientException rcex = ex as RawClientException; TypeInitializationException tiex = ex as TypeInitializationException; //Rzucenie wyjątkiem w konstruktorze wywoła wyjątek TypeInitializationException //Jeśli jest wywołany obsłużonym wyjątkiem to taki zostanie przetworzony if (tiex != null && tiex.InnerException != null) { ClientException innercex = tiex.InnerException as ClientException; if (innercex != null) { cex = innercex; } } if (sqlEx != null && sqlEx.Number == -2) { cex = new ClientException(ClientExceptionId.SqlTimeout); } if (cex != null) { var exNode = from node in exceptionsTemplates.Root.Elements() where node.Attribute("id").Value == cex.Id.ToString() select node; //copy exception template nodes to the final exception xml foreach (XElement element in exNode.ElementAt(0).Elements()) { exceptionXml.Root.Add(element); } foreach (XAttribute attribute in exNode.ElementAt(0).Attributes()) { exceptionXml.Root.Add(attribute); } // //inject parameters into exception xml if (cex.Parameters != null) { foreach (string parameter in cex.Parameters) { //dont use split because parameter value can include ':' int delimiterIndex = parameter.IndexOf(':'); string key = parameter.Substring(0, delimiterIndex); string value = parameter.Substring(delimiterIndex + 1, parameter.Length - delimiterIndex - 1); foreach (XElement element in exceptionXml.Root.Descendants()) { if (!element.HasElements) { element.Value = element.Value.Replace("%" + key + "%", value); } } } } if (cex.XmlData != null) { exceptionXml.Root.Add(cex.XmlData); } if (ConfigurationMapper.Instance.LogHandledExceptions) { ServiceHelper.Instance.LogException(ex); } else if (cex.InnerException != null) { ServiceHelper.Instance.LogException(cex.InnerException); } } else if (rcex != null) { exceptionXml.Root.Add(new XElement("customMessage", rcex.Message)); if (ConfigurationMapper.Instance.LogHandledExceptions) { ServiceHelper.Instance.LogException(ex); } } else { //if its an unhandled exception var exNode = from node in exceptionsTemplates.Root.Elements() where node.Attribute("id").Value == "UNHANDLED_EXCEPTION" select node; //copy exception template nodes to the final exception xml foreach (XElement element in exNode.ElementAt(0).Elements()) { exceptionXml.Root.Add(element); } foreach (XAttribute attribute in exNode.ElementAt(0).Attributes()) { exceptionXml.Root.Add(attribute); } // exceptionXml.Root.Element("message").Value = ex.Message; exceptionXml.Root.Element("className").Value = ex.GetType().ToString(); exceptionXml.Root.Element("serverVersion").Value = ServiceHelper.Instance.GetVersion(); //if (ex.InnerException != null) //{ // exceptionXml.Root.Add(ServiceHelper.Instance.CreateInnerExceptionXml(ex.InnerException)); //} int logNumber = ServiceHelper.Instance.LogException(ex); if (logNumber > 0) { exceptionXml.Root.Element("logNumber").Value = logNumber.ToString(CultureInfo.InvariantCulture); } else { exceptionXml.Root.Element("logNumber").Remove(); } } //leave only one language string userLang = null; if (SessionManager.SessionId != null) { try { userLang = SessionManager.Language; } catch (Exception) { RoboFramework.Tools.RandomLogHelper.GetLog().Debug("EXCEPTION: (KernelServices) What is this exception?"); } } if (userLang == null) { userLang = this.GetClientLanguageVersion(); } var localizableNodes = from node in exceptionXml.Root.Elements() where node.Attribute("lang") != null group node by node.Name.LocalName into g select g; foreach (var nodesGroup in localizableNodes) { var preferredLang = from node in nodesGroup where node.Attribute("lang").Value == userLang select node; XElement preferredElement = null; if (preferredLang.Count() > 0) { preferredElement = preferredLang.ElementAt(0); } else //select the first one { preferredElement = nodesGroup.ElementAt(0); } //delete the others foreach (XElement element in nodesGroup) { if (element != preferredElement) { element.Remove(); } } } return(exceptionXml); }