using System; using System.Collections.Generic; using System.Configuration; using Sentry; 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) { if (additionalData == null) { additionalData = new Dictionary(); } if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SentryDsn"])) { SentrySdk.WithScope(scope => { if (HttpContextHelper.UserName != null) { scope.User = new Sentry.User { Email = HttpContextHelper.UserName }; } scope.SetTags(customTags); scope.SetExtras(additionalData); SentrySdk.CaptureException(ex); }); } } #endregion #region ProcessError public static string ProcessError(Exception ex, string className, string method, Dictionary additionalData = null) { string error = ""; #region Send error var customTags = new Dictionary(); customTags.Add("Class", className); customTags.Add("Method", method); ErrorHelper.LogError(ex, customTags, additionalData); #endregion #region Clean Error and Return The Error error = GetError(ex); #endregion return error; } #endregion } }