private bool TryDeMinify(string package, int line, int column, out CallStackFrameInfo callStackFrameInfo)
        {
            AjaxMinSymbolForJavaScript javaScriptSymbol = default(AjaxMinSymbolForJavaScript);

            javaScriptSymbol.ScriptEndLine     = line;
            javaScriptSymbol.ScriptStartLine   = line;
            javaScriptSymbol.ScriptEndColumn   = column;
            javaScriptSymbol.ScriptStartColumn = column;
            AjaxMinSymbolForJavaScript ajaxMinSymbolForJavaScript;

            if (!this.parameters.MinificationSymbolsMapForJavaScript.Search(package, javaScriptSymbol, out ajaxMinSymbolForJavaScript))
            {
                callStackFrameInfo = null;
                return(false);
            }
            callStackFrameInfo = new CallStackFrameInfo
            {
                StartLine    = ajaxMinSymbolForJavaScript.SourceStartLine,
                StartColumn  = ajaxMinSymbolForJavaScript.SourceStartColumn,
                EndLine      = ajaxMinSymbolForJavaScript.SourceEndLine,
                EndColumn    = ajaxMinSymbolForJavaScript.SourceEndColumn,
                FunctionName = this.parameters.MinificationSymbolsMapForJavaScript.GetFunctionName(ajaxMinSymbolForJavaScript.FunctionNameIndex),
                FileName     = ClientWatsonDatapointHandler.GetRelativeFilePath(this.parameters.MinificationSymbolsMapForJavaScript.GetSourceFilePathFromId(ajaxMinSymbolForJavaScript.SourceFileId)),
                FolderPath   = this.parameters.ExchangeSourcesPath
            };
            return(true);
        }
        public void ReportWatsonEvents(UserContext userContext, IList <ClientLogEvent> watsonEvents, Datapoint contextHeader, Datapoint[] context)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            int       num       = 0;
            int       num2      = 0;
            List <ClientWatsonDatapointHandler.ClientWatsonReportLogEvent> list = new List <ClientWatsonDatapointHandler.ClientWatsonReportLogEvent>(watsonEvents.Count);
            List <WatsonClientReport> list2 = new List <WatsonClientReport>(watsonEvents.Count);

            foreach (ClientLogEvent clientLogEvent in watsonEvents)
            {
                string ctqName;
                clientLogEvent.TryGetValue <string>("ctq", out ctqName);
                ClientWatsonReportData reportData;
                ProcessingResult       errorResult = this.TryExtractClientWatsonData(clientLogEvent, out reportData);
                if (errorResult.Code != ResultCode.Success)
                {
                    list.Add(new ClientWatsonDatapointHandler.ClientWatsonReportLogEvent(clientLogEvent.Time, userContext, ctqName, reportData.IsUnhandledException, errorResult));
                    num2++;
                }
                else
                {
                    int  hashCode = reportData.OriginalCallStack.GetHashCode();
                    bool flag     = this.parameters.IsErrorOverReportQuota(hashCode);
                    if (flag)
                    {
                        string[] watsonParameters;
                        if (ClientWatsonDatapointHandler.cachedFlavor == null)
                        {
                            ClientWatsonDatapointHandler.CreateClientWatsonReportAndUpdateCache(reportData, out watsonParameters);
                        }
                        else
                        {
                            watsonParameters = WatsonClientReport.BuildWatsonParameters(ClientWatsonDatapointHandler.cachedFlavor, ClientWatsonDatapointHandler.cachedVersion, reportData.TraceComponent, reportData.FunctionName, reportData.ExceptionType, reportData.NormalizedCallStack, reportData.CallStackHash);
                        }
                        list.Add(new ClientWatsonDatapointHandler.ClientWatsonReportLogEvent(clientLogEvent.Time, userContext, ctqName, watsonParameters, reportData.IsUnhandledException, false));
                        num++;
                    }
                    else
                    {
                        string[]           watsonParameters;
                        WatsonClientReport item = ClientWatsonDatapointHandler.CreateClientWatsonReportAndUpdateCache(reportData, out watsonParameters);
                        list2.Add(item);
                        list.Add(new ClientWatsonDatapointHandler.ClientWatsonReportLogEvent(clientLogEvent.Time, userContext, ctqName, watsonParameters, reportData.IsUnhandledException, true));
                    }
                }
            }
            string extraData = ClientWatsonDatapointHandler.BuildExtraDataString(userContext, contextHeader, context, list);

            foreach (WatsonClientReport report in list2)
            {
                this.parameters.ReportAction(report, extraData);
            }
            this.logger.Set(LogDatapointMetadata.WatsonDatapointSkipped, num);
            this.logger.Set(LogDatapointMetadata.WatsonDatapointFailed, num2);
            this.logger.Set(LogDatapointMetadata.WatsonReportingElapsed, stopwatch.ElapsedMilliseconds);
        }
        private static string BuildExtraDataString(UserContext userContext, Datapoint contextHeader, Datapoint[] context, List <ClientWatsonDatapointHandler.ClientWatsonReportLogEvent> eventsToLog)
        {
            string        text          = ClientWatsonDatapointHandler.FormatClientLogDataForWatson(contextHeader, context);
            StringBuilder stringBuilder = new StringBuilder(text.Length + "------------------------ClientLogs------------------------".Length + "---------------Watson events on this session--------------".Length);
            string        text2         = (userContext != null) ? userContext.LogEventCommonData.Features : null;
            string        text3         = (userContext != null) ? userContext.LogEventCommonData.Flights : null;

            stringBuilder.AppendLine("-------------------Features & Flights---------------------");
            stringBuilder.Append("Features: ");
            stringBuilder.AppendLine(string.IsNullOrEmpty(text2) ? "None" : text2);
            stringBuilder.Append("Flights: ");
            stringBuilder.AppendLine(string.IsNullOrEmpty(text3) ? "None" : text3);
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("---------------Watson events on this session--------------");
            if (userContext != null)
            {
                foreach (string value in userContext.GetClientWatsonHistory())
                {
                    stringBuilder.AppendLine(value);
                    stringBuilder.AppendLine();
                }
            }
            string[] array = new string[eventsToLog.Count];
            int      num   = 0;

            foreach (ClientWatsonDatapointHandler.ClientWatsonReportLogEvent clientWatsonReportLogEvent in eventsToLog)
            {
                OwaClientLogger.AppendToLog(clientWatsonReportLogEvent);
                string text4 = string.Format("{0}{1}{2}", clientWatsonReportLogEvent, Environment.NewLine, clientWatsonReportLogEvent.GetWatsonUrl());
                array[num++] = text4;
                stringBuilder.AppendLine(text4);
                stringBuilder.AppendLine();
            }
            if (userContext != null)
            {
                Array.Sort <string>(array);
                userContext.SaveToClientWatsonHistory(array);
            }
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("------------------------ClientLogs------------------------");
            stringBuilder.AppendLine(text);
            return(stringBuilder.ToString());
        }
        private ResultCode TryDeMinifyAndDeObfuscate(string package, int line, int column, out CallStackFrameInfo callStackFrameInfo)
        {
            AjaxMinSymbolForScriptSharp javaScriptSymbol = default(AjaxMinSymbolForScriptSharp);

            javaScriptSymbol.ScriptEndLine     = line;
            javaScriptSymbol.ScriptStartLine   = line;
            javaScriptSymbol.ScriptEndColumn   = column;
            javaScriptSymbol.ScriptStartColumn = column;
            AjaxMinSymbolForScriptSharp ajaxMinSymbolForScriptSharp;

            if (!this.parameters.MinificationSymbolsMapForScriptSharp.Search(package, javaScriptSymbol, out ajaxMinSymbolForScriptSharp))
            {
                callStackFrameInfo = null;
                return(ResultCode.FailedToDeMinify);
            }
            ScriptSharpSymbolWrapper javaScriptSymbol2 = new ScriptSharpSymbolWrapper(new ScriptSharpSymbol
            {
                ScriptStartPosition = ajaxMinSymbolForScriptSharp.SourceStartPosition,
                ScriptEndPosition   = ajaxMinSymbolForScriptSharp.SourceEndPosition
            });
            ScriptSharpSymbolWrapper scriptSharpSymbolWrapper;

            if (!this.parameters.ObfuscationSymbolsMap.Search(package, javaScriptSymbol2, out scriptSharpSymbolWrapper))
            {
                callStackFrameInfo = null;
                return(ResultCode.FailedToDeObfuscate);
            }
            ScriptSharpSymbol innerSymbol = scriptSharpSymbolWrapper.InnerSymbol;

            callStackFrameInfo = new CallStackFrameInfo
            {
                StartLine    = innerSymbol.SourceStartLine,
                FunctionName = this.parameters.ObfuscationSymbolsMap.GetFunctionName(innerSymbol.FunctionNameIndex),
                FileName     = ClientWatsonDatapointHandler.GetRelativeFilePath(this.parameters.ObfuscationSymbolsMap.GetSourceFilePathFromId(innerSymbol.SourceFileId)),
                FolderPath   = this.parameters.ExchangeSourcesPath
            };
            return(ResultCode.Success);
        }
        public ProcessingResult TryProcessCallStackFrame(string frame, out CallStackFrameInfo callStackFrameInfo)
        {
            List <Regex> regexList = new List <Regex>
            {
                ClientWatsonDatapointHandler.ChromeAndIEStackFrameRegex,
                ClientWatsonDatapointHandler.IOS7StackFrameRegex
            };

            callStackFrameInfo = null;
            Match match;

            if (!ClientWatsonDatapointHandler.TryMatchAny(frame, regexList, out match))
            {
                return(new ProcessingResult
                {
                    Code = ResultCode.NoMatch,
                    RawErrorFrame = frame
                });
            }
            string text = match.Groups["package"].Value;
            int    num  = text.LastIndexOf('/') + 1;

            if (num > 0)
            {
                text = text.Substring(num, text.Length - num);
            }
            int              num2  = int.Parse(match.Groups["line"].Value);
            int              num3  = int.Parse(match.Groups["column"].Value);
            string           text2 = match.Groups["function"].Value.TrimEnd(new char[0]);
            string           text3;
            Tuple <int, int> tuple;

            if (this.parameters.ConsolidationSymbolsMap.HasSymbolsLoadedForScript(text))
            {
                if (!this.parameters.ConsolidationSymbolsMap.Search(text, num2, num3, out text3, out tuple))
                {
                    return(new ProcessingResult
                    {
                        Code = ResultCode.FailedToDeConsolidate,
                        Package = text,
                        Function = text2,
                        Line = num2,
                        Column = num3
                    });
                }
            }
            else
            {
                text3 = text;
                tuple = new Tuple <int, int>(num2, num3);
            }
            if (this.parameters.MinificationSymbolsMapForScriptSharp.HasSymbolsLoadedForScript(text3))
            {
                ResultCode resultCode = this.TryDeMinifyAndDeObfuscate(text3, tuple.Item1, tuple.Item2, out callStackFrameInfo);
                if (resultCode != ResultCode.Success)
                {
                    return(new ProcessingResult
                    {
                        Code = resultCode,
                        SourceType = FrameSourceType.ScriptSharp,
                        Package = text3,
                        Function = text2,
                        Line = tuple.Item1,
                        Column = tuple.Item2
                    });
                }
            }
            else if (this.parameters.MinificationSymbolsMapForJavaScript.HasSymbolsLoadedForScript(text3))
            {
                if (!this.TryDeMinify(text3, tuple.Item1, tuple.Item2, out callStackFrameInfo))
                {
                    return(new ProcessingResult
                    {
                        Code = ResultCode.FailedToDeMinify,
                        SourceType = FrameSourceType.JavaScript,
                        Package = text3,
                        Function = text2,
                        Line = tuple.Item1,
                        Column = tuple.Item2
                    });
                }
            }
            else
            {
                callStackFrameInfo = new CallStackFrameInfo(true)
                {
                    StartLine    = tuple.Item1,
                    EndLine      = tuple.Item1,
                    StartColumn  = tuple.Item2,
                    EndColumn    = tuple.Item2,
                    FunctionName = text2,
                    FileName     = text3 + ".js",
                    FolderPath   = string.Empty
                };
            }
            callStackFrameInfo.PackageName = text3;
            return(new ProcessingResult
            {
                Code = ResultCode.Success
            });
        }
        public ProcessingResult TryExtractClientWatsonData(ClientLogEvent watsonEvent, out ClientWatsonReportData reportData)
        {
            reportData = default(ClientWatsonReportData);
            string text;

            if (watsonEvent.TryGetValue <string>("BuildType", out text))
            {
                return(new ProcessingResult
                {
                    Code = ResultCode.DebugBuild
                });
            }
            bool flag = watsonEvent.TryGetValue <string>("est", out reportData.OriginalCallStack);

            if (!flag && !watsonEvent.TryGetValue <string>("st", out reportData.OriginalCallStack))
            {
                return(new ProcessingResult
                {
                    Code = ResultCode.NoStackTrace
                });
            }
            reportData.IsUnhandledException = new bool?(flag);
            if (!reportData.OriginalCallStack.Contains(this.parameters.OwaVersion))
            {
                return(new ProcessingResult
                {
                    Code = ResultCode.VersionMismatch
                });
            }
            if (!watsonEvent.TryGetValue <string>("s", out reportData.TraceComponent))
            {
                return(new ProcessingResult
                {
                    Code = ResultCode.NoTraceComponent
                });
            }
            StringBuilder stringBuilder = new StringBuilder(reportData.OriginalCallStack.Length);
            bool          flag2         = true;
            string        functionName  = null;
            string        packageName   = null;

            foreach (string frame in ClientWatsonDatapointHandler.EnumerateFrames(reportData.OriginalCallStack))
            {
                CallStackFrameInfo callStackFrameInfo;
                ProcessingResult   result = this.TryProcessCallStackFrame(frame, out callStackFrameInfo);
                if (result.Code != ResultCode.Success)
                {
                    return(result);
                }
                if (flag2)
                {
                    functionName = callStackFrameInfo.SanitizedFunctionName;
                    packageName  = callStackFrameInfo.PackageName;
                }
                if (reportData.FunctionName == null && !ClientWatsonDatapointHandler.ExternalScriptRegex.IsMatch(callStackFrameInfo.PackageName) && (flag || !flag2))
                {
                    reportData.FunctionName = callStackFrameInfo.SanitizedFunctionName;
                    reportData.PackageName  = callStackFrameInfo.PackageName;
                }
                callStackFrameInfo.UpdateHash(ref reportData.CallStackHash);
                stringBuilder.AppendFormat(callStackFrameInfo.ToString(), new object[0]);
                flag2 = false;
            }
            if (!flag2)
            {
                reportData.NormalizedCallStack = stringBuilder.ToString();
                if (!watsonEvent.TryGetValue <string>("em", out reportData.ExceptionMessage))
                {
                    reportData.ExceptionMessage = "unknown";
                }
                if (!watsonEvent.TryGetValue <string>("en", out reportData.ExceptionType))
                {
                    string text2;
                    if (!flag && watsonEvent.TryGetValue <string>("f", out text2) && !string.IsNullOrEmpty(text2))
                    {
                        reportData.ExceptionType    = ClientWatsonDatapointHandler.SanitizeExceptionType(text2);
                        reportData.ExceptionMessage = ClientWatsonDatapointHandler.FormatExceptionMessage(text2, watsonEvent);
                    }
                    else
                    {
                        reportData.ExceptionType = "unknown";
                    }
                }
                if (string.IsNullOrEmpty(reportData.FunctionName))
                {
                    reportData.FunctionName = functionName;
                    reportData.PackageName  = packageName;
                }
                return(new ProcessingResult
                {
                    Code = ResultCode.Success
                });
            }
            return(new ProcessingResult
            {
                Code = ResultCode.NoFrames
            });
        }