Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface ILogger
- {
- void Exception(string message, Exception ex);
- void Info(string message);
- void Warning(string message);
- void Progress(ProgressDataEventArgs progressData); //new method required for logging progress
- }
- public class MemoryLogger : ILogger
- {
- public List<LogLine> Log { get; }
- private readonly ILogger _internalLogger; //internal logger that this logger will decorate
- public MemoryLogger(ILogger internalLogger)
- {
- Log = new List<LogLine>();
- _internalLogger = internalLogger;
- }
- public void Exception(string message, Exception ex)
- {
- Log.Add(new LogLine(LogLine.SeverityLevels.Error, message, ex));
- _internalLogger.Exception(message, ex); //each public method does its job and calls internal logger method (which may do the same).
- }
- public void Info(string message)
- {
- Log.Add(new LogLine(LogLine.SeverityLevels.Info, message));
- _internalLogger.Info(message);
- }
- public void Warning(string message)
- {
- Log.Add(new LogLine(LogLine.SeverityLevels.Warn, message));
- _internalLogger.Warning(message);
- }
- public void Progress(ProgressDataEventArgs progressData)
- {
- _internalLogger.Progress(progressData);
- }
- public override string ToString()
- {
- var sb = new StringBuilder();
- foreach (var logLine in Log)
- sb.AppendLine(logLine.ToString());
- return sb.ToString();
- }
- }
- public class CmdletLogger : ILogger
- {
- private readonly Cmdlet _cmdlet;
- public CmdletLogger(Cmdlet cmdlet)
- {
- _cmdlet = cmdlet;
- }
- public void Exception(string message, Exception ex)
- {
- _cmdlet.WriteError(new ErrorRecord(ex, Guid.NewGuid().ToString(), ErrorCategory.NotSpecified, message));
- }
- public void Info(string message)
- {
- _cmdlet.WriteInformation(message, new string[] { });
- }
- public void Warning(string message)
- {
- _cmdlet.WriteWarning(message);
- }
- public void Progress(ProgressDataEventArgs progressData)
- {
- var progressRecord = new ProgressRecord(progressData.ActivityId, progressData.Activity, progressData.StatusDescription)
- {
- PercentComplete = progressData.AllStepsCount == 0 ? 100 : (progressData.CurrentStepNo * 100 / progressData.AllStepsCount),
- RecordType = ProgressRecordType.Processing
- };
- _cmdlet.WriteProgress(progressRecord);
- }
- }
- public class ProgressDataEventArgs : EventArgs
- {
- public int ActivityId { get; }
- public string Activity { get; }
- public string StatusDescription { get; }
- public int CurrentStepNo { get; }
- public int AllStepsCount { get; }
- public ProgressDataEventArgs(int activityId, string activity, string statusDescription, int currentStepNo, int allStepsCount)
- {
- ActivityId = activityId;
- Activity = activity;
- StatusDescription = statusDescription;
- CurrentStepNo = currentStepNo;
- AllStepsCount = allStepsCount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement