Example #1
0
 /// <summary>
 /// Use Oracle
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public static ExceptionOptions UseOracle(this ExceptionOptions options)
 {
     options.UseDestucturer <OracleExceptionDestructurer>();
     options.UseDestucturer <OracleLpExceptionDestructurer>();
     options.UseDestucturer <OracleTypeExceptionDestructurer>();
     return(options);
 }
        public static void AppendCollection(this StringBuilder sb, string propertyName, IEnumerable collection, ExceptionOptions options)
        {
            sb.AppendLine(string.Format("{0}{1}:", options.Indent, propertyName).PadRight(23));

            var innerOptions = new ExceptionOptions(options, options.CurrentIndentLevel + 1);

            var i = 0;

            foreach (var item in collection)
            {
                var innerPropertyName = string.Format("[{0}]", i);

                if (item is Exception)
                {
                    var innerException = (Exception)item;
                    sb.AppendException(innerPropertyName, innerException, innerOptions);
                }
                else
                {
                    sb.AppendValue(innerPropertyName, item, innerOptions);
                }

                ++i;
            }
        }
 /// <summary>
 /// Create a new instance of <see cref="ValidationException"/>.
 /// </summary>
 /// <param name="options"></param>
 public ValidationException(ExceptionOptions options) : base(options)
 {
     if (options != null && options.ExtraErrors.TryGetValue(ValidationMessageInfoKey, out var value) && value is IEnumerable <string> messages)
     {
         _validationMessage = messages;
     }
 }
Example #4
0
        public static string ToDetailedString(this Exception exception, ExceptionOptions options)
        {
            var stringBuilder = new StringBuilder();

            AppendValue(stringBuilder, "Type", exception.GetType().FullName, options);

            foreach (PropertyInfo property in exception
                     .GetType()
                     .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                     .Where(p => p.CanRead)
                     .OrderByDescending(x => string.Equals(x.Name, nameof(exception.Message), StringComparison.Ordinal))
                     .ThenByDescending(x => string.Equals(x.Name, nameof(exception.Source), StringComparison.Ordinal))
                     .ThenBy(x => string.Equals(x.Name, nameof(exception.InnerException), StringComparison.Ordinal))
                     .ThenBy(x => string.Equals(x.Name, nameof(AggregateException.InnerExceptions), StringComparison.Ordinal)))
            {
                var value = property.GetValue(exception, null);
                if (value == null && options.OmitNullProperties)
                {
                    if (options.OmitNullProperties)
                    {
                        continue;
                    }
                    else
                    {
                        value = string.Empty;
                    }
                }

                AppendValue(stringBuilder, property.Name, value, options);
            }

            return(stringBuilder.ToString().TrimEnd('\r', '\n'));
        }
Example #5
0
        /// <summary>
        /// Creates the logger for the application
        /// </summary>
        private static ILogger CreateLogger(IConfigurationRoot configuration)
        {
            var loggerConfig = new LoggerConfiguration()
                               .MinimumLevel.Debug()
                               .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                               .MinimumLevel.Override("System", LogEventLevel.Information)
                               .Enrich.FromLogContext()
                               .WriteTo.Async(c => c.File(
                                                  "../Logs/appLog_.txt",
                                                  outputTemplate: GetLoggingTemplate(),
                                                  rollingInterval: RollingInterval.Day
                                                  ));

            ExceptionOptions errorSettings = configuration.GetSection("exceptions").Get <ExceptionOptions>();

            if (String.IsNullOrEmpty(errorSettings.MailTo) == false)
            {
                EmailConnectionInfo emailInfo = new EmailConnectionInfo {
                    EmailSubject = $"Seed Project: Unhandled Exception occurred on machine {Environment.MachineName}",
                    MailServer   = errorSettings.SmtpServer,
                    FromEmail    = errorSettings.MailFrom,
                    ToEmail      = errorSettings.MailTo
                };
                loggerConfig.WriteTo.Email(emailInfo, GetLoggingTemplate(), LogEventLevel.Error);
            }

#if DEBUG
            //we will only use the console logger when debugging
            loggerConfig.WriteTo.Console(outputTemplate: GetLoggingTemplate());
#endif
            return(loggerConfig.CreateLogger());
        }
Example #6
0
 private static void AppendValue(
     StringBuilder stringBuilder,
     string propertyName,
     object?value,
     ExceptionOptions options)
 {
     if (value is DictionaryEntry dictionaryEntry)
     {
         stringBuilder.AppendLine($"{options.Indent}{propertyName} = {dictionaryEntry.Key} : {dictionaryEntry.Value}");
     }
     else if (value is Exception innerException)
     {
         AppendException(
             stringBuilder,
             propertyName,
             innerException,
             options);
     }
     else if (value is IEnumerable collection && !(value is string))
     {
         if (collection.GetEnumerator().MoveNext())
         {
             AppendCollection(
                 stringBuilder,
                 propertyName,
                 collection,
                 options);
         }
     }
 public FinalDestructuringOptions(ExceptionOptions options, ExceptionConfiguration configuration)
 {
     Name             = DestructuringOptionsGetter.GetRealRootName(configuration, options);
     DestructureDepth = DestructuringOptionsGetter.GetRealDepth(configuration, options);
     Destructurers    = options.Destructurers;
     Filter           = options.ExceptionPropertyFilter;
 }
 public bool?ShowDialog(Exception exception, ExceptionOptions options, string message)
 {
     SetupButtonVisibility(options);
     Exception = exception;
     Message   = message;
     ShowDialog();
     return(status);
 }
Example #9
0
        public static IApplicationBuilder UseCustomExceptionHandler(this IApplicationBuilder builder,
                                                                    Action <ExceptionOptions> configureOptions)
        {
            var options = new ExceptionOptions();

            configureOptions(options);

            return(builder.UseMiddleware <ExceptionMiddleware>(options));
        }
Example #10
0
        public void RaiseExceptionWithOptions()
        {
            var options = new ExceptionOptions
            {
                InnerException = new InvalidOperationException("invalid ops"),
                ErrorCode      = 1000,
                Flag           = "TEST_ERR"
            };

            Assert.Throws <ArgumentNullException>(() => ExceptionBuilder.Raise <ValidationException>(false, (ExceptionOptions)null));
            Assert.Throws <ValidationException>(() => ExceptionBuilder.Raise <ValidationException>(false, options));
        }
Example #11
0
        private static void AppendException(
            StringBuilder stringBuilder,
            string propertyName,
            Exception exception,
            ExceptionOptions options)
        {
            var innerExceptionString = ToDetailedString(
                exception,
                new ExceptionOptions(options, options.CurrentIndentLevel + 1));

            stringBuilder.AppendLine($"{options.Indent}{propertyName} =");
            stringBuilder.AppendLine(innerExceptionString);
        }
Example #12
0
        public void ExceptionOptionsCreatingTest()
        {
            var options = new ExceptionOptions
            {
                InnerException = new InvalidOperationException("invalid ops"),
                ErrorCode      = 1000,
                Flag           = "TEST_ERR"
            };
            var lvZ = new ValidationException(options);

            lvZ.InnerException.ShouldNotBeNull();
            lvZ.InnerException.Message.ShouldBe("invalid ops");
            lvZ.Code.ShouldBe(1000);
            lvZ.Flag.ShouldBe("TEST_ERR");
        }
Example #13
0
        /// <summary>
        /// Add exception integration for Cosmos Logging
        /// </summary>
        /// <param name="service"></param>
        /// <param name="exceptionOptionAct"></param>
        /// <param name="configAction"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static ILogServiceCollection AddExceptionsIntegration(
            this ILogServiceCollection service,
            Action <ExceptionOptions> exceptionOptionAct = null,
            Action <IConfiguration, ExceptionConfiguration> configAction = null)
        {
            if (service is null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            var settings = ExceptionOptions.Create(exceptionOptionAct);

            service.UseExceptionExtensionsCore(settings.ToMsOptions(), (conf, sink, _) => configAction?.Invoke(conf, sink));

            return(service);
        }
Example #14
0
        /// <summary>
        /// Add exception integration for Cosmos Logging
        /// </summary>
        /// <param name="service"></param>
        /// <param name="exceptionOptionAct"></param>
        /// <param name="configAction"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static ILogServiceCollection AddExceptionsIntegration(
            this ILogServiceCollection service,
            Action <ExceptionOptions> exceptionOptionAct = null,
            Action <IConfiguration, ExceptionConfiguration> configAction = null)
        {
            if (service is null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            var settings = new ExceptionOptions();

            exceptionOptionAct?.Invoke(settings);

            //string GetExpectLevelName() => settings.MinimumLevel.HasValue ? settings.MinimumLevel.Value.GetName() : LogEventLevel.Verbose.GetName();

            return(UseExceptionExtensionsCore(service, Options.Create(settings), (conf, sink, _) => configAction?.Invoke(conf, sink)));
        }
        private void SetupButtonVisibility(ExceptionOptions options)
        {
            switch (options)
            {
            case ExceptionOptions.Continue:
                ContinueButtonVisibility = Visibility.Visible;
                ExitButtonVisibility     = Visibility.Collapsed;
                break;

            case ExceptionOptions.Exit:
                ContinueButtonVisibility = Visibility.Collapsed;
                ExitButtonVisibility     = Visibility.Visible;
                break;

            case ExceptionOptions.ExitOrContinue:
                ContinueButtonVisibility = Visibility.Visible;
                ExitButtonVisibility     = Visibility.Visible;
                break;
            }
        }
Example #16
0
    private static void AppendCollection(
        StringBuilder stringBuilder,
        string propertyName,
        IEnumerable collection,
        ExceptionOptions options)
    {
        stringBuilder.AppendLine($"{options.Indent}{propertyName} =");

        var innerOptions = new ExceptionOptions(options, options.CurrentIndentLevel + 1);

        var i = 0;

        foreach (var item in collection)
        {
            var innerPropertyName = $"[{i}]";

            if (item is Exception)
            {
                var innerException = (Exception)item;
                AppendException(
                    stringBuilder,
                    innerPropertyName,
                    innerException,
                    innerOptions);
            }
            else
            {
                AppendValue(
                    stringBuilder,
                    innerPropertyName,
                    item,
                    innerOptions);
            }

            ++i;
        }
    }
 /// <summary>
 /// Use SQLite
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public static ExceptionOptions UseSqlite(this ExceptionOptions options)
 {
     options.UseDestucturer <SqliteExceptionDestructurer>();
     return(options);
 }
Example #18
0
 /// <summary>
 /// Use EntityFrameworkCore
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public static ExceptionOptions UseEntityFrameworkCore(this ExceptionOptions options)
 {
     options.UseDestucturer <EfCoreDbUpdateExceptionDestructurer>();
     return(options);
 }
 /// <summary>
 /// Create a new cosmos exception instance.
 /// </summary>
 /// <param name="options"></param>
 protected CosmosException(ExceptionOptions options) : base(options.Message, options.InnerException)
 {
     ExtraData = options.ExtraErrors;
     Code      = options.ErrorCode;
     Flag      = options.Flag;
 }
 public bool?ShowDialog(Exception exception, ExceptionOptions options)
 {
     return(ShowDialog(exception, options, string.Empty));
 }
Example #21
0
 internal ExceptionOptions(ExceptionOptions options, int currentIndent)
 {
     this.CurrentIndentLevel = currentIndent;
     this.IndentSpaces       = options.IndentSpaces;
     this.OmitNullProperties = options.OmitNullProperties;
 }
Example #22
0
 private static string IndentString(string value, ExceptionOptions options)
 {
     return(value.Replace(Environment.NewLine, Environment.NewLine + options.Indent));
 }
 /// <summary>
 /// Use MySql
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public static ExceptionOptions UseMySql(this ExceptionOptions options)
 {
     options.UseDestucturer <MySqlExceptionDestructurer>();
     return(options);
 }
        public static void AppendException(this StringBuilder sb, string propertyName, Exception ex, ExceptionOptions options)
        {
            var innerExceptionString = ex.ToString(new ExceptionOptions(options, options.CurrentIndentLevel + 1));

            sb.AppendLine(string.Format("{0}{1}: ", options.Indent, propertyName).PadRight(23));
            sb.AppendLine(innerExceptionString);
        }
 public static FinalDestructuringOptions Create(ExceptionOptions options, ExceptionConfiguration configuration)
 => new FinalDestructuringOptions(options, configuration);
 public static void AppendValue(this StringBuilder sb, string propertyName, object value, ExceptionOptions options)
 {
     if (value is Exception)
     {
         var innerException = (Exception)value;
         sb.AppendException(propertyName, innerException, options);
     }
     else if (value is StackTrace)
     {
         sb.Append(string.Format("{0}{1}:", options.Indent, propertyName).PadRight(23));
         sb.AppendLine(((StackTrace)value).ToString(true));
     }
     else if (value is IEnumerable && !(value is string))
     {
         var collection = (IEnumerable)value;
         if (collection.GetEnumerator().MoveNext())
         {
             sb.AppendCollection(propertyName, collection, options);
         }
     }
     else
     {
         sb.Append(string.Format("{0}{1}:", options.Indent, propertyName).PadRight(23));
         if (value is DictionaryEntry)
         {
             DictionaryEntry dictionaryEntry = (DictionaryEntry)value;
             sb.AppendLine(string.Format("{0} : {1}", dictionaryEntry.Key, dictionaryEntry.Value));
         }
         else if (propertyName == "HResult")
         {
             sb.AppendLine(string.Format("0x{0:X}", (int)value));
         }
         else
         {
             sb.AppendLine(value.ToString());
         }
     }
 }
 /// <summary>
 /// Use Postgres
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public static ExceptionOptions UsePostgreSql(this ExceptionOptions options)
 {
     options.UseDestucturer <NpgsqlExceptionDestructurer>();
     options.UseDestucturer <PostgresExceptionDestructurer>();
     return(options);
 }