Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- namespace Import.Data
- {
- /// <summary>
- /// Class that stores execution times, messages and other
- /// information required to generate exception report for
- /// import process
- /// </summary>
- public class SyncResultItemNew : IDisposable
- {
- #region Private Variables
- private readonly List<SyncResultItemNew> _childern;
- private readonly Dictionary<string, string> _parameters;
- private readonly bool _shouldFallThrough;
- private readonly Stopwatch _stopWatch;
- private string _action;
- private string _description;
- private TimeSpan _duration;
- private string _logicalStep;
- private SyncResultType _resultType;
- #endregion
- #region PrivateMethods
- private string SubstituteParamName(string paramName)
- {
- string baseParamName = paramName;
- string actualParamName = paramName;
- int index = 0;
- while (_parameters.ContainsKey(actualParamName))
- {
- actualParamName = baseParamName + "(" + ++index + ")";
- }
- return actualParamName;
- }
- #endregion
- #region Constructor
- /// <param name="stepName">Name of the step.</param>
- /// <remarks>
- /// If step name is not provided then
- /// step name is set to "Unknown"
- /// </remarks>
- private SyncResultItemNew(string stepName)
- {
- _logicalStep = !string.IsNullOrEmpty(stepName) ? stepName : "Unknown";
- _stopWatch = Stopwatch.StartNew();
- _parameters = new Dictionary<string, string>();
- _childern = new List<SyncResultItemNew>();
- _action = string.Empty;
- _description = string.Empty;
- _shouldFallThrough = false;
- _resultType = SyncResultType.Success;
- }
- #endregion
- #region Public Properties
- /// <summary>
- /// Gets a value indicating whether code processing should fall through or not.
- /// </summary>
- /// <value><c>true</c> if code processing should fall through otherwise, <c>false</c>.</value>
- public bool ShouldFallThrough
- {
- get { return _shouldFallThrough; }
- }
- #endregion
- #region IDisposable Members
- void IDisposable.Dispose()
- {
- if (_stopWatch != null)
- {
- _stopWatch.Stop();
- _duration = _stopWatch.Elapsed;
- }
- }
- #endregion
- #region Public Methods
- /// <summary>
- /// Creates a parent that stores all the necessary information required
- /// for exception report and stores execution times of all the logical steps.
- /// </summary>
- /// <param name="stepName">Name of the step.</param>
- /// <returns>
- /// A newly created instance of the <see cref="SyncResultItemNew"/> class
- /// that stores all information required for exception reporting after
- /// import process.
- /// </returns>
- /// <exception cref="ObjectDisposedException">Object is already disposed.</exception>
- /// <remarks>
- /// If step name is not provided, then root is created with name "Unknown".
- /// </remarks>
- public static SyncResultItemNew CreateRoot(string stepName)
- {
- return new SyncResultItemNew(stepName);
- }
- /// <summary>
- /// Creates a child of <see cref="SyncResultItemNew"/> name with logical step
- /// under a parent <see cref="SyncResultItemNew"/>.
- /// </summary>
- /// <param name="stepName">Name of the step.</param>
- /// <returns>
- /// A newly created child instance of the <see cref="SyncResultItemNew"/> class
- /// that stores all information required for exception reporting after
- /// import process under a parent.
- /// </returns>
- /// <exception cref="ObjectDisposedException">Object is already disposed.</exception>
- /// <remarks>
- /// If no step name is provided then child will be
- /// created with step name "Unknown".
- /// </remarks>
- public SyncResultItemNew CreateChild(string stepName)
- {
- var syncResultItemNew = new SyncResultItemNew(stepName);
- _childern.Add(syncResultItemNew);
- return syncResultItemNew;
- }
- /// <param name="paramName">Name of the parameter.</param>
- /// <param name="paramValue">Value of the parameter</param>
- /// <remarks>
- /// If parameter name is not provided then parameter name would be defaulted
- /// to "Key". If an existing parameter name is provided then code
- /// increments the parameter name and then add it to collection
- /// </remarks>
- public void AddParam(string paramName, string paramValue)
- {
- paramName = string.IsNullOrEmpty(paramName) ? "Key" : paramName;
- if (!_parameters.ContainsKey(paramName))
- {
- _parameters.Add(paramName, paramValue);
- }
- else
- {
- _parameters.Add(SubstituteParamName(paramName), paramValue);
- }
- }
- /// <param name="ex">The exception that needs to be added to collection</param>
- /// <remarks>
- /// If proper exception is not passed, then the exception
- /// messages are ignored/not stored. If this method is called more
- /// than once within the the scope/context of the object and exception is
- /// provided exception messages are added to collection with an incremented key.
- /// </remarks>
- public void AddUnexpectedError(Exception ex)
- {
- if (ex != null)
- {
- AddParam("Message", ex.Message);
- }
- }
- /// <param name="syncResultType">Type of the sync result.</param>
- /// <param name="description">The description that needs to be added.</param>
- /// <remarks>
- /// Populates result type, description. If description is not passed then
- /// description is substituted with default description, "No Description".
- /// If this method is called more than once within the the scope/context of the
- /// object then previous description and result type are overwritten.
- /// </remarks>
- public void AddResult(SyncResultType syncResultType, string description)
- {
- _resultType = syncResultType;
- _description = !string.IsNullOrEmpty(description) ? description : "No Description";
- }
- /// <param name="syncResultType">Type of sync result.</param>
- /// <param name="description">Description that needs to be set.</param>
- /// <param name="ex">The exception that needs to be added to collection.</param>
- /// <remarks>
- /// Populates result type, description and exception
- /// If description is not passed then description is substituted with default
- /// description "No Description". If proper exception is not passed then exception
- /// messages are ignored and not added to collection. If this method is called more than
- /// once within the the scope/context of the object then previous description and result
- /// type are overwritten. If exception is provided new exception messages are
- /// stored with an incremented key.
- /// </remarks>
- public void AddResult(SyncResultType syncResultType, string description, Exception ex)
- {
- _resultType = syncResultType;
- _description = !string.IsNullOrEmpty(description) ? description : "No Description";
- if (ex != null)
- {
- AddParam("Message", ex.Message);
- }
- }
- #endregion
- }
- }
No comments:
Post a Comment