Documentation Coverage Report
Current view: top level - Cqrs.WebApi/Formatters/FormMultipart/Infrastructure/Logger - FormDataConverterLogger.cs Hit Total Coverage
Version: 2.2 Artefacts: 10 10 100.0 %
Date: 2018-08-07 15:04:50

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="Chinchilla Software Limited">
       4             : // //   Copyright Chinchilla Software Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using System.Collections.Generic;
      11             : using System.Linq;
      12             : 
      13             : namespace Cqrs.WebApi.Formatters.FormMultipart.Infrastructure.Logger
      14             : {
      15             :         /// <summary>
      16             :         /// Provide a mechanism to log issues and <see cref="Exception"/> data during conversions.
      17             :         /// </summary>
      18             :         public class FormDataConverterLogger : IFormDataConverterLogger
      19           1 :         {
      20             :                 private Dictionary<string, List<LogErrorInfo>> Errors { get; set; }
      21             : 
      22             :                 /// <summary>
      23             :                 /// Instantiates a new instance of <see cref="FormDataConverterLogger"/>.
      24             :                 /// </summary>
      25           1 :                 public FormDataConverterLogger()
      26             :                 {
      27             :                         Errors = new Dictionary<string, List<LogErrorInfo>>();
      28             :                 }
      29             : 
      30             :                 /// <summary>
      31             :                 /// Logs an error.
      32             :                 /// </summary>
      33             :                 /// <param name="errorPath">The path to the member for which the error is being logged.</param>
      34             :                 /// <param name="exception">The exception to be logged.</param>
      35           1 :                 public void LogError(string errorPath, Exception exception)
      36             :                 {
      37             :                         AddError(errorPath, new LogErrorInfo(exception));
      38             :                 }
      39             : 
      40             :                 /// <summary>
      41             :                 /// Logs an error.
      42             :                 /// </summary>
      43             :                 /// <param name="errorPath">The path to the member for which the error is being logged.</param>
      44             :                 /// <param name="errorMessage">The error message to be logged.</param>
      45           1 :                 public void LogError(string errorPath, string errorMessage)
      46             :                 {
      47             :                         AddError(errorPath, new LogErrorInfo(errorMessage));
      48             :                 }
      49             :                 
      50             :                 /// <summary>
      51             :                 /// Get all errors recorded.
      52             :                 /// </summary>
      53           1 :                 public List<LogItem> GetErrors()
      54             :                 {
      55             :                         return Errors.Select
      56             :                         (
      57             :                                 m => new LogItem
      58             :                                 {
      59             :                                         ErrorPath = m.Key,
      60             :                                         Errors = m.Value.Select(t => t).ToList()
      61             :                                 }
      62             :                         ).ToList();
      63             :                 }
      64             : 
      65             :                 /// <summary>
      66             :                 /// Throw exception if errors found
      67             :                 /// </summary>
      68           1 :                 public void EnsureNoErrors()
      69             :                 {
      70             :                         if (Errors.Any())
      71             :                         {
      72             :                                 var errors = Errors
      73             :                                         .Select(m => String.Format("{0}: {1}", m.Key, String.Join(". ", m.Value.Select(x => (x.ErrorMessage ?? (x.Exception != null ? x.Exception.Message : ""))))))
      74             :                                         .ToList();
      75             : 
      76             :                                 string errorMessage = String.Join(" ", errors);
      77             : 
      78             :                                 throw new Exception(errorMessage);
      79             :                         }
      80             :                 }
      81             : 
      82             :                 private void AddError(string errorPath, LogErrorInfo info)
      83             :                 {
      84             :                         List<LogErrorInfo> listErrors;
      85             :                         if (!Errors.TryGetValue(errorPath, out listErrors))
      86             :                         {
      87             :                                 listErrors = new List<LogErrorInfo>();
      88             :                                 Errors.Add(errorPath, listErrors);
      89             :                         }
      90             :                         listErrors.Add(info);
      91             :                 }
      92             : 
      93             :                 /// <summary>
      94             :                 /// Errors for a given path.
      95             :                 /// </summary>
      96             :                 public class LogItem
      97           1 :                 {
      98             :                         /// <summary>
      99             :                         /// The path identifying where the <see cref="Exception"/> or issue occurred.
     100             :                         /// </summary>
     101             :                         public string ErrorPath { get; set; }
     102             : 
     103             :                         /// <summary>
     104             :                         /// All <see cref="Exception">exceptions</see> or issues that occurred for the <see cref="ErrorPath"/>.
     105             :                         /// </summary>
     106             :                         public List<LogErrorInfo> Errors { get; set; }
     107             :                 }
     108             : 
     109             :                 /// <summary>
     110             :                 /// An error, issue or <see cref="Exception"/>.
     111             :                 /// </summary>
     112             :                 public class LogErrorInfo
     113           1 :                 {
     114             :                         /// <summary>
     115             :                         /// The details of the error, issue or <see cref="Exception"/>.
     116             :                         /// </summary>
     117             :                         public string ErrorMessage { get; private set; }
     118             : 
     119             :                         /// <summary>
     120             :                         /// The <see cref="Exception"/> if <see cref="IsException"/> is true.
     121             :                         /// </summary>
     122             :                         public Exception Exception { get; private set; }
     123             : 
     124             :                         /// <summary>
     125             :                         /// Indicates of the error or issue was an exception.
     126             :                         /// </summary>
     127             :                         public bool IsException { get; private set; }
     128             : 
     129             :                         /// <summary>
     130             :                         /// Instantiates a new instance of <see cref="FormDataConverterLogger"/> with the specified <paramref name="errorMessage"/>.
     131             :                         /// </summary>
     132           1 :                         public LogErrorInfo(string errorMessage)
     133             :                         {
     134             :                                 ErrorMessage = errorMessage;
     135             :                                 IsException = false;
     136             :                         }
     137             : 
     138             :                         /// <summary>
     139             :                         /// Instantiates a new instance of <see cref="FormDataConverterLogger"/> with the specified <paramref name="exception"/>
     140             :                         /// </summary>
     141           1 :                         public LogErrorInfo(Exception exception)
     142             :                         {
     143             :                                 Exception = exception;
     144             :                                 IsException = true;
     145             :                         }
     146             :                 }
     147             :         }
     148             : }

Generated by: LCOV version 1.12