Advertisement
pszczyg

DecoratorLogger_2

Jul 28th, 2021
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1.  public interface ILogger
  2.     {
  3.         void Exception(string message, Exception ex);
  4.         void Info(string message);
  5.         void Warning(string message);
  6.         void Progress(ProgressDataEventArgs progressData); //new method required for logging progress
  7.     }
  8.  
  9.     public class MemoryLogger : ILogger
  10.     {
  11.         public List<LogLine> Log { get; }
  12.         private readonly ILogger _internalLogger; //internal logger that this logger will decorate
  13.  
  14.         public MemoryLogger(ILogger internalLogger)
  15.         {
  16.             Log = new List<LogLine>();
  17.             _internalLogger = internalLogger;
  18.         }
  19.         public void Exception(string message, Exception ex)
  20.         {
  21.             Log.Add(new LogLine(LogLine.SeverityLevels.Error, message, ex));
  22.             _internalLogger.Exception(message, ex); //each public method does its job and calls internal logger method (which may do the same).
  23.         }
  24.  
  25.         public void Info(string message)
  26.         {
  27.             Log.Add(new LogLine(LogLine.SeverityLevels.Info, message));
  28.             _internalLogger.Info(message);
  29.         }
  30.  
  31.         public void Warning(string message)
  32.         {
  33.             Log.Add(new LogLine(LogLine.SeverityLevels.Warn, message));
  34.             _internalLogger.Warning(message);
  35.         }
  36.  
  37.         public void Progress(ProgressDataEventArgs progressData)
  38.         {
  39.             _internalLogger.Progress(progressData);
  40.         }
  41.        
  42.         public override string ToString()
  43.         {
  44.             var sb = new StringBuilder();
  45.             foreach (var logLine in Log)
  46.                 sb.AppendLine(logLine.ToString());
  47.             return sb.ToString();
  48.         }
  49.     }
  50.  
  51.     public class CmdletLogger : ILogger
  52.     {
  53.         private readonly Cmdlet _cmdlet;
  54.  
  55.         public CmdletLogger(Cmdlet cmdlet)
  56.         {
  57.             _cmdlet = cmdlet;
  58.         }
  59.  
  60.         public void Exception(string message, Exception ex)
  61.         {
  62.             _cmdlet.WriteError(new ErrorRecord(ex, Guid.NewGuid().ToString(), ErrorCategory.NotSpecified, message));
  63.         }
  64.  
  65.         public void Info(string message)
  66.         {
  67.             _cmdlet.WriteInformation(message, new string[] { });
  68.         }
  69.  
  70.         public void Warning(string message)
  71.         {
  72.             _cmdlet.WriteWarning(message);
  73.         }
  74.  
  75.         public void Progress(ProgressDataEventArgs progressData)
  76.         {
  77.             var progressRecord = new ProgressRecord(progressData.ActivityId, progressData.Activity, progressData.StatusDescription)
  78.             {
  79.                 PercentComplete = progressData.AllStepsCount == 0 ? 100 : (progressData.CurrentStepNo * 100 / progressData.AllStepsCount),
  80.                 RecordType = ProgressRecordType.Processing
  81.             };
  82.             _cmdlet.WriteProgress(progressRecord);
  83.         }
  84.     }
  85.  
  86.  public class ProgressDataEventArgs : EventArgs
  87.     {
  88.         public int ActivityId { get; }
  89.         public string Activity { get; }
  90.         public string StatusDescription { get; }
  91.         public int CurrentStepNo { get; }
  92.         public int AllStepsCount { get; }
  93.  
  94.         public ProgressDataEventArgs(int activityId, string activity, string statusDescription, int currentStepNo, int allStepsCount)
  95.         {
  96.             ActivityId = activityId;
  97.             Activity = activity;
  98.             StatusDescription = statusDescription;
  99.             CurrentStepNo = currentStepNo;
  100.             AllStepsCount = allStepsCount;
  101.         }
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement