/// <summary> /// Publish a <see cref="RxLogEntry"/> to the host. /// </summary> /// <param name="entry"></param> public void Publish(RxLogEntry entry) { Guard.NotNull(entry); if (Enabled) { LogHost.Publish(entry); } }
/// <summary> /// Processes the process phase of the cmdlet /// </summary> protected override void ProcessRecord() { #region Perform Transforms if ((!_fromStopFunction) && (Target != null)) { Target = ResolveTarget(Target); } if (!_fromStopFunction) { if (Exception != null) { Exception = ResolveException(Exception); } else if (ErrorRecord != null) { Exception tempException = null; for (int n = 0; n < ErrorRecord.Length; n++) { // If both Exception and ErrorRecord are specified, override the first error record's exception. if ((n == 0) && (Exception != null)) { tempException = Exception; } else { tempException = ResolveException(ErrorRecord[n].Exception); } if (tempException != ErrorRecord[n].Exception) { ErrorRecord[n] = new ErrorRecord(tempException, ErrorRecord[n].FullyQualifiedErrorId, ErrorRecord[n].CategoryInfo.Category, ErrorRecord[n].TargetObject); } } } } if (Level != MessageLevel.Warning) { Level = ResolveLevel(Level); } #endregion Perform Transforms #region Exception Integration /* * While conclusive error handling must happen after message handling, * in order to integrate the exception message into the actual message, * it becomes necessary to first integrate the exception and error record parameters into a uniform view * * Note: Stop-PSFFunction never specifies this parameter, thus it is not necessary to check, * whether this function was called from Stop-PSFFunction. */ if ((ErrorRecord == null) && (Exception != null)) { ErrorRecord = new ErrorRecord[1]; ErrorRecord[0] = new ErrorRecord(Exception, String.Format("{0}_{1}", ModuleName, FunctionName), ErrorCategory.NotSpecified, Target); } #endregion Exception Integration #region Error handling PsfExceptionRecord errorRecord = null; if (ErrorRecord != null) { if (!_fromStopFunction) { if (EnableException) { foreach (ErrorRecord record in ErrorRecord) { WriteError(record); } } } errorRecord = LogHost.WriteErrorEntry(ErrorRecord, FunctionName, ModuleName, _Tags, _timestamp, _MessageSystem, System.Management.Automation.Runspaces.Runspace.DefaultRunspace.InstanceId, Environment.MachineName); } #endregion Error handling LogEntryType channels = LogEntryType.None; #region Warning handling if (Level == MessageLevel.Warning) { if (!_silent) { if (!String.IsNullOrEmpty(Once)) { string onceName = String.Format("MessageOnce.{0}.{1}", FunctionName, Once).ToLower(); if (!(Configuration.ConfigurationHost.Configurations.ContainsKey(onceName) && (bool)Configuration.ConfigurationHost.Configurations[onceName].Value)) { WriteWarning(_MessageStreams); channels = channels | LogEntryType.Warning; Configuration.Config cfg = new Configuration.Config(); cfg.Module = "messageonce"; cfg.Name = String.Format("{0}.{1}", FunctionName, Once).ToLower(); cfg.Hidden = true; cfg.Description = "Locking setting that disables further display of the specified message"; cfg.Value = true; Configuration.ConfigurationHost.Configurations[onceName] = cfg; } } else { WriteWarning(_MessageStreams); channels = channels | LogEntryType.Warning; } } WriteDebug(_MessageStreams); channels = channels | LogEntryType.Debug; } #endregion Warning handling #region Message handling if (!_silent) { if ((MessageHost.MaximumInformation >= (int)Level) && (MessageHost.MinimumInformation <= (int)Level)) { if (!String.IsNullOrEmpty(Once)) { string onceName = String.Format("MessageOnce.{0}.{1}", FunctionName, Once).ToLower(); if (!(Configuration.ConfigurationHost.Configurations.ContainsKey(onceName) && (bool)Configuration.ConfigurationHost.Configurations[onceName].Value)) { InvokeCommand.InvokeScript(false, ScriptBlock.Create(_writeHostScript), null, _MessageHost); channels = channels | LogEntryType.Information; Configuration.Config cfg = new Configuration.Config(); cfg.Module = "messageonce"; cfg.Name = String.Format("{0}.{1}", FunctionName, Once).ToLower(); cfg.Hidden = true; cfg.Description = "Locking setting that disables further display of the specified message"; cfg.Value = true; Configuration.ConfigurationHost.Configurations[onceName] = cfg; } } else { //InvokeCommand.InvokeScript(_writeHostScript, _MessageHost); InvokeCommand.InvokeScript(false, ScriptBlock.Create(_writeHostScript), null, _MessageHost); channels = channels | LogEntryType.Information; } } } if ((MessageHost.MaximumVerbose >= (int)Level) && (MessageHost.MinimumVerbose <= (int)Level)) { if ((_callStack.Count() > 1) && _callStack.ElementAt(_callStack.Count() - 2).InvocationInfo.BoundParameters.ContainsKey("Verbose")) { InvokeCommand.InvokeScript(@"$VerbosePreference = 'Continue'"); } //SessionState.PSVariable.Set("VerbosePreference", ActionPreference.Continue); WriteVerbose(_MessageStreams); channels = channels | LogEntryType.Verbose; } if ((MessageHost.MaximumDebug >= (int)Level) && (MessageHost.MinimumDebug <= (int)Level)) { bool restoreInquire = false; if (_isDebug) { if (Breakpoint.ToBool()) { InvokeCommand.InvokeScript(false, ScriptBlock.Create(@"$DebugPreference = 'Inquire'"), null, null); } else { InvokeCommand.InvokeScript(false, ScriptBlock.Create(@"$DebugPreference = 'Continue'"), null, null); restoreInquire = true; } WriteDebug(String.Format("{0} | {1}", Line, _MessageStreams)); channels = channels | LogEntryType.Debug; } else { WriteDebug(_MessageStreams); channels = channels | LogEntryType.Debug; } if (restoreInquire) { InvokeCommand.InvokeScript(false, ScriptBlock.Create(@"$DebugPreference = 'Inquire'"), null, null); } } #endregion Message handling #region Logging LogEntry entry = LogHost.WriteLogEntry(_MessageSystem, channels, _timestamp, FunctionName, ModuleName, _Tags, Level, System.Management.Automation.Runspaces.Runspace.DefaultRunspace.InstanceId, Environment.MachineName, File, Line, _callStack, String.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName), errorRecord, String, StringValues, Target); #endregion Logging foreach (MessageEventSubscription subscription in MessageHost.Events.Values) { if (subscription.Applies(entry)) { try { InvokeCommand.InvokeScript(subscription.ScriptBlock.ToString(), entry); } catch (Exception e) { WriteError(new ErrorRecord(e, "", ErrorCategory.NotSpecified, entry)); } } } }