using System; using System.Collections.Generic; using System.Configuration; using Sentry; using System.Net; namespace Neo.Afx.Common { public static class ErrorHelper { private static IDisposable _sentry; #region GetInnerMostError public static Exception GetInnerMostError(Exception ex) { while (ex.InnerException != null) { ex = ex.InnerException; } return ex; } #endregion #region GetError public static string GetError(Exception ex) { string error = "An error has occured"; ex = GetInnerMostError(ex); if (!String.IsNullOrEmpty(ex.Message)) { if (ex.Message.StartsWith("#EXPOSE_ERROR#")) { error = ex.Message.Replace("#EXPOSE_ERROR#", ""); } } return error; } #endregion #region LogError public static void LogError(Exception ex, Dictionary customTags, Dictionary additionalData = null) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; if (additionalData == null) additionalData = new Dictionary(); var dsn = ConfigurationManager.AppSettings["SentryDsn"]; _sentry = SentrySdk.Init(options => { options.Dsn = ConfigurationManager.AppSettings["SentryDsn"]; options.Debug = true; options.AutoSessionTracking = true; options.IsGlobalModeEnabled = true; options.EnableTracing = true; }); SentrySdk.CaptureException(ex); } #endregion #region ProcessError public static string ProcessError(Exception ex, string className, string method, Dictionary additionalData = null) { #region Send error var customTags = new Dictionary { { "Class", className }, { "Method", method } }; ErrorHelper.LogError(ex, customTags, additionalData); #endregion #region Clean Error and Return The Error var error = GetError(ex); #endregion return error; } #endregion } }