Beispiel #1
0
        /// <summary>
        /// Enrich logger output with a destuctured object containing
        /// exception's public properties.
        /// </summary>
        /// <param name="loggerEnrichmentConfiguration">The enrichment configuration</param>
        /// <param name="destructuringOptions">
        /// Options that will influence the process of destructuring
        /// exception's properties into result object.
        /// </param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static Serilog.LoggerConfiguration WithExceptionDetails(
            this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
            IDestructuringOptions destructuringOptions)
        {
            ILogEventEnricher enricher = new ExceptionEnricher(destructuringOptions);

            return(loggerEnrichmentConfiguration.With(enricher));
        }
        /// <summary>
        /// Enrich logger output with a destuctured object containing exception's public properties.
        /// </summary>
        /// <param name="loggerEnrichmentConfiguration">The enrichment configuration.</param>
        /// <param name="destructuringOptions">
        /// Options that will influence the process of destructuring exception's properties into result object.
        /// </param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration WithExceptionDetails(
            this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
            IDestructuringOptions destructuringOptions)
        {
            if (loggerEnrichmentConfiguration is null)
            {
                throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
            }

            ILogEventEnricher enricher = new ExceptionEnricher(destructuringOptions);

            return(loggerEnrichmentConfiguration.With(enricher));
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExceptionEnricher"/> class.
        /// </summary>
        /// <param name="destructuringOptions">The destructuring options, cannot be null.</param>
        public ExceptionEnricher(IDestructuringOptions destructuringOptions)
        {
            this.destructuringOptions        = destructuringOptions ?? throw new ArgumentNullException(nameof(destructuringOptions));
            this.reflectionBasedDestructurer = new ReflectionBasedDestructurer(destructuringOptions.DestructuringDepth);

            this.destructurers = new Dictionary <Type, IExceptionDestructurer>();
            foreach (var destructurer in this.destructuringOptions.Destructurers)
            {
                foreach (var targetType in destructurer.TargetTypes)
                {
                    this.destructurers.Add(targetType, destructurer);
                }
            }
        }
Beispiel #4
0
        /// <inheritdoc />
        public ExceptionDestructuringProcessor()
        {
            _destructuringOptions = FinalDestructuringOptions.Current;
            if (_destructuringOptions == null)
            {
                throw new ArgumentException("Final destructuring options cannot be null.",
                                            nameof(FinalDestructuringOptions.Current));
            }

            _reflectionBasedDestructurer = new ReflectionBasedDestructurer(_destructuringOptions.DestructureDepth);
            _destructurers = new Dictionary <Type, IExceptionDestructurer>();
            foreach (var destructurer in _destructuringOptions.Destructurers)
            {
                foreach (var targetType in destructurer.TargetTypes)
                {
                    _destructurers.Add(targetType, destructurer);
                }
            }
        }
        public static JObject LogAndDestructureException(
            Exception exception,
            IDestructuringOptions destructuringOptions = null)
        {
            // Arrange
            var jsonWriter = new StringWriter();

            destructuringOptions ??= new DestructuringOptionsBuilder().WithDefaultDestructurers();
            ILogger logger = new LoggerConfiguration()
                             .Enrich.WithExceptionDetails(destructuringOptions)
                             .WriteTo.Sink(new TestTextWriterSink(jsonWriter, new JsonFormatter()))
                             .CreateLogger();

            // Act
            logger.Error(exception, "EXCEPTION MESSAGE");

            // Assert
            var writtenJson = jsonWriter.ToString();
            var jsonObj     = JsonConvert.DeserializeObject <object>(writtenJson);
            var rootObject  = Assert.IsType <JObject>(jsonObj);

            return(rootObject);
        }
 public static bool IsDefaultName(this IDestructuringOptions options)
 {
     return(1 == string.Compare(Constants.RootName, options.Name, StringComparison.Ordinal));
 }
 public static bool IsDefaultDepth(this IDestructuringOptions options)
 {
     return(Constants.DefaultDestructureDepth == options.DestructureDepth);
 }
Beispiel #8
0
 public AsyncErrorLoggingInterceptor(ILogger logger)
 {
     m_Logger = logger ?? throw new ArgumentNullException(nameof(logger));
     m_DestructuringOptions = new DestructuringOptionsBuilder().WithDefaultDestructurers();
 }