/// <summary> /// Provider interface function for logging provider health event /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="exception"></param> /// internal abstract void LogProviderHealthEvent(LogContext logContext, string providerName, Exception exception);
private static EventLogEntryType GetEventLogEntryType(LogContext logContext) { switch (logContext.Severity) { case "Critical": case "Error": return EventLogEntryType.Error; case "Warning": return EventLogEntryType.Warning; default: return EventLogEntryType.Information; } }
/// <summary> /// Provider interface function for logging health event /// </summary> /// <param name="logContext"></param> /// <param name="eventId"></param> /// <param name="exception"></param> /// <param name="additionalInfo"></param> /// internal abstract void LogEngineHealthEvent(LogContext logContext, int eventId, Exception exception, Dictionary<String, String> additionalInfo);
/// <summary> /// Provider interface function for logging command health event /// </summary> /// <param name="logContext"></param> /// <param name="exception"></param> internal abstract void LogCommandHealthEvent(LogContext logContext, Exception exception);
/// <summary> /// LogEngineHealthEvent: This is an API for logging engine health event while execution context /// is not available. In this case, caller of this API will directly construct LogContext /// instance. /// /// This API is currently used only by runspace before engine start. /// </summary> /// <param name="logContext">logContext to be </param> /// <param name="eventId">EventId for the event to be logged</param> /// <param name="exception">Exception associated with this event</param> /// <param name="additionalInfo">Additional information for this event</param> internal static void LogEngineHealthEvent(LogContext logContext, int eventId, Exception exception, Dictionary<String, String> additionalInfo ) { if (logContext == null) { PSTraceSource.NewArgumentNullException("logContext"); return; } if (exception == null) { PSTraceSource.NewArgumentNullException("exception"); return; } // Here execution context doesn't exist, we will have to log this event regardless. // Don't check NeedToLogEngineHealthEvent here. foreach (LogProvider provider in GetLogProvider(logContext)) { provider.LogEngineHealthEvent(logContext, eventId, exception, additionalInfo); } }
/// <summary> /// DummyLogProvider does nothing to Logging ProviderLifecycleEvent /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="newState"></param> /// internal override void LogProviderLifecycleEvent(LogContext logContext, string providerName, ProviderState newState) { }
/// <summary> /// DummyLogProvider does nothing to Logging SettingsEvent. /// </summary> /// <param name="logContext"></param> /// <param name="variableName"></param> /// <param name="value"></param> /// <param name="previousValue"></param> internal override void LogSettingsEvent(LogContext logContext, string variableName, string value, string previousValue) { }
/// <summary> /// Log settings event. /// </summary> /// <param name="logContext"></param> /// <param name="variableName"></param> /// <param name="value"></param> /// <param name="previousValue"></param> internal override void LogSettingsEvent(LogContext logContext, string variableName, string value, string previousValue) { int eventId = _settingsEventId; Hashtable mapArgs = new Hashtable(); mapArgs["VariableName"] = variableName; mapArgs["NewValue"] = value; mapArgs["PreviousValue"] = previousValue; FillEventArgs(mapArgs, logContext); EventInstance entry = new EventInstance(eventId, SettingsCategoryId); entry.EntryType = EventLogEntryType.Information; string detail = GetEventDetail("SettingsContext", mapArgs); LogEvent(entry, variableName, value, previousValue, detail); }
/// <summary> /// Provider interface function for logging provider health event. /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="exception"></param> internal override void LogProviderHealthEvent(LogContext logContext, string providerName, Exception exception) { }
/// <summary> /// DummyLogProvider does nothing to Logging ProviderLifecycleEvent. /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="newState"></param> internal override void LogProviderLifecycleEvent(LogContext logContext, string providerName, ProviderState newState) { }
/// <summary> /// DummyLogProvider does nothing to Logging PipelineExecutionDetailEvent. /// </summary> /// <param name="logContext"></param> /// <param name="pipelineExecutionDetail"></param> internal override void LogPipelineExecutionDetailEvent(LogContext logContext, List <string> pipelineExecutionDetail) { }
/// <summary> /// Provider interface function for logging command health event. /// </summary> /// <param name="logContext"></param> /// <param name="exception"></param> internal override void LogCommandHealthEvent(LogContext logContext, Exception exception) { }
/// <summary> /// DummyLogProvider does nothing to Logging EngineLifecycleEvent. /// </summary> /// <param name="logContext"></param> /// <param name="newState"></param> /// <param name="previousState"></param> internal override void LogEngineLifecycleEvent(LogContext logContext, EngineState newState, EngineState previousState) { }
/// <summary> /// Log pipeline execution detail event. /// /// This may end of logging more than one event if the detail string is too long to be fit in 64K. /// </summary> /// <param name="logContext"></param> /// <param name="pipelineExecutionDetail"></param> internal override void LogPipelineExecutionDetailEvent(LogContext logContext, List<String> pipelineExecutionDetail) { List<String> details = GroupMessages(pipelineExecutionDetail); for (int i = 0; i < details.Count; i++) { LogPipelineExecutionDetailEvent(logContext, details[i], i + 1, details.Count); } }
/// <summary> /// Provider interface function for logging health event. /// </summary> /// <param name="logContext"></param> /// <param name="eventId"></param> /// <param name="exception"></param> /// <param name="additionalInfo"></param> internal abstract void LogEngineHealthEvent(LogContext logContext, int eventId, Exception exception, Dictionary <string, string> additionalInfo);
/// <summary> /// Provider interface function for logging provider health event /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="exception"></param> /// internal override void LogProviderHealthEvent(LogContext logContext, string providerName, Exception exception) { int eventId = _providerHealthEventId; Hashtable mapArgs = new Hashtable(); mapArgs["ProviderName"] = providerName; IContainsErrorRecord icer = exception as IContainsErrorRecord; if (null != icer && null != icer.ErrorRecord) { mapArgs["ExceptionClass"] = exception.GetType().Name; mapArgs["ErrorCategory"] = icer.ErrorRecord.CategoryInfo.Category; mapArgs["ErrorId"] = icer.ErrorRecord.FullyQualifiedErrorId; if (icer.ErrorRecord.ErrorDetails != null && !String.IsNullOrEmpty(icer.ErrorRecord.ErrorDetails.Message)) { mapArgs["ErrorMessage"] = icer.ErrorRecord.ErrorDetails.Message; } else { mapArgs["ErrorMessage"] = exception.Message; } } else { mapArgs["ExceptionClass"] = exception.GetType().Name; mapArgs["ErrorCategory"] = ""; mapArgs["ErrorId"] = ""; mapArgs["ErrorMessage"] = exception.Message; } FillEventArgs(mapArgs, logContext); EventInstance entry = new EventInstance(eventId, ProviderHealthCategoryId); entry.EntryType = GetEventLogEntryType(logContext); string detail = GetEventDetail("ProviderHealthContext", mapArgs); LogEvent(entry, mapArgs["ErrorMessage"], detail); }
/// <summary> /// Provider interface function for logging engine lifecycle event. /// </summary> /// <param name="logContext"></param> /// <param name="newState"></param> /// <param name="previousState"></param> internal abstract void LogEngineLifecycleEvent(LogContext logContext, EngineState newState, EngineState previousState);
/// <summary> /// Log engine health event /// </summary> /// <param name="logContext"></param> /// <param name="eventId"></param> /// <param name="exception"></param> /// <param name="additionalInfo"></param> internal override void LogEngineHealthEvent(LogContext logContext, int eventId, Exception exception, Dictionary<String, String> additionalInfo) { Hashtable mapArgs = new Hashtable(); IContainsErrorRecord icer = exception as IContainsErrorRecord; if (null != icer && null != icer.ErrorRecord) { mapArgs["ExceptionClass"] = exception.GetType().Name; mapArgs["ErrorCategory"] = icer.ErrorRecord.CategoryInfo.Category; mapArgs["ErrorId"] = icer.ErrorRecord.FullyQualifiedErrorId; if (icer.ErrorRecord.ErrorDetails != null) { mapArgs["ErrorMessage"] = icer.ErrorRecord.ErrorDetails.Message; } else { mapArgs["ErrorMessage"] = exception.Message; } } else { mapArgs["ExceptionClass"] = exception.GetType().Name; mapArgs["ErrorCategory"] = ""; mapArgs["ErrorId"] = ""; mapArgs["ErrorMessage"] = exception.Message; } FillEventArgs(mapArgs, logContext); FillEventArgs(mapArgs, additionalInfo); EventInstance entry = new EventInstance(eventId, EngineHealthCategoryId); entry.EntryType = GetEventLogEntryType(logContext); string detail = GetEventDetail("EngineHealthContext", mapArgs); LogEvent(entry, mapArgs["ErrorMessage"], detail); }
/// <summary> /// Provider interface function for logging command health event. /// </summary> /// <param name="logContext"></param> /// <param name="exception"></param> internal abstract void LogCommandHealthEvent(LogContext logContext, Exception exception);
/// <summary> /// Provider interface function for logging settings event. /// </summary> /// <param name="logContext"></param> /// <param name="variableName"></param> /// <param name="value"></param> /// <param name="previousValue"></param> internal abstract void LogSettingsEvent(LogContext logContext, string variableName, string value, string previousValue);
/// <summary> /// Provider interface function for logging pipeline execution detail. /// </summary> /// <param name="logContext"></param> /// <param name="pipelineExecutionDetail"></param> internal abstract void LogPipelineExecutionDetailEvent(LogContext logContext, List <string> pipelineExecutionDetail);
/// <summary> /// DummyLogProvider does nothing to Logging SettingsEvent /// </summary> /// <param name="logContext"></param> /// <param name="variableName"></param> /// <param name="value"></param> /// <param name="previousValue"></param> /// internal override void LogSettingsEvent(LogContext logContext, string variableName, string value, string previousValue) { }
/// <summary> /// Provider interface function for logging provider health event. /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="exception"></param> internal abstract void LogProviderHealthEvent(LogContext logContext, string providerName, Exception exception);
/// <summary> /// Provider interface function for logging engine lifecycle event /// </summary> /// <param name="logContext"></param> /// <param name="newState"></param> /// <param name="previousState"></param> /// internal abstract void LogEngineLifecycleEvent(LogContext logContext, EngineState newState, EngineState previousState);
/// <summary> /// Provider interface function for logging provider lifecycle event. /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="newState"></param> internal abstract void LogProviderLifecycleEvent(LogContext logContext, string providerName, ProviderState newState);
/// <summary> /// Provider interface function for logging pipeline execution detail. /// </summary> /// <param name="logContext"></param> /// <param name="pipelineExecutionDetail"></param> internal abstract void LogPipelineExecutionDetailEvent(LogContext logContext, List<String> pipelineExecutionDetail);
/// <summary> /// Generate LogContext structure based on executionContext and invocationInfo passed in. /// /// LogContext structure is used in log provider interface. /// </summary> /// <param name="executionContext"></param> /// <param name="invocationInfo"></param> /// <param name="severity"></param> /// <returns></returns> private static LogContext GetLogContext(ExecutionContext executionContext, InvocationInfo invocationInfo, Severity severity) { if (executionContext == null) { return(null); } LogContext logContext = new LogContext(); string shellId = executionContext.ShellID; logContext.ExecutionContext = executionContext; logContext.ShellId = shellId; logContext.Severity = severity.ToString(); if (executionContext.EngineHostInterface != null) { logContext.HostName = executionContext.EngineHostInterface.Name; logContext.HostVersion = executionContext.EngineHostInterface.Version.ToString(); logContext.HostId = (string)executionContext.EngineHostInterface.InstanceId.ToString(); } logContext.HostApplication = String.Join(" ", Environment.GetCommandLineArgs()); if (executionContext.CurrentRunspace != null) { logContext.EngineVersion = executionContext.CurrentRunspace.Version.ToString(); logContext.RunspaceId = executionContext.CurrentRunspace.InstanceId.ToString(); Pipeline currentPipeline = ((RunspaceBase)executionContext.CurrentRunspace).GetCurrentlyRunningPipeline(); if (currentPipeline != null) { logContext.PipelineId = currentPipeline.InstanceId.ToString(CultureInfo.CurrentCulture); } } logContext.SequenceNumber = NextSequenceNumber; try { if (executionContext.LogContextCache.User == null) { logContext.User = Environment.UserDomainName + "\\" + Environment.UserName; executionContext.LogContextCache.User = logContext.User; } else { logContext.User = executionContext.LogContextCache.User; } } catch (InvalidOperationException) { logContext.User = Logging.UnknownUserName; } System.Management.Automation.Remoting.PSSenderInfo psSenderInfo = executionContext.SessionState.PSVariable.GetValue("PSSenderInfo") as System.Management.Automation.Remoting.PSSenderInfo; if (psSenderInfo != null) { logContext.ConnectedUser = psSenderInfo.UserInfo.Identity.Name; } logContext.Time = DateTime.Now.ToString(CultureInfo.CurrentCulture); if (invocationInfo == null) { return(logContext); } logContext.ScriptName = invocationInfo.ScriptName; logContext.CommandLine = invocationInfo.Line; if (invocationInfo.MyCommand != null) { logContext.CommandName = invocationInfo.MyCommand.Name; logContext.CommandType = invocationInfo.MyCommand.CommandType.ToString(); switch (invocationInfo.MyCommand.CommandType) { case CommandTypes.Application: logContext.CommandPath = ((ApplicationInfo)invocationInfo.MyCommand).Path; break; case CommandTypes.ExternalScript: logContext.CommandPath = ((ExternalScriptInfo)invocationInfo.MyCommand).Path; break; } } return(logContext); }
/// <summary> /// Provider interface function for logging provider lifecycle event /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="newState"></param> /// internal abstract void LogProviderLifecycleEvent(LogContext logContext, string providerName, ProviderState newState);
/// <summary> /// Provider interface function for logging settings event /// </summary> /// <param name="logContext"></param> /// <param name="variableName"></param> /// <param name="value"></param> /// <param name="previousValue"></param> /// internal abstract void LogSettingsEvent(LogContext logContext, string variableName, string value, string previousValue);
/// <summary> /// Log engine lifecycle event /// </summary> /// <param name="logContext"></param> /// <param name="newState"></param> /// <param name="previousState"></param> internal override void LogEngineLifecycleEvent(LogContext logContext, EngineState newState, EngineState previousState) { int eventId = GetEngineLifecycleEventId(newState); if (eventId == _invalidEventId) return; Hashtable mapArgs = new Hashtable(); mapArgs["NewEngineState"] = newState.ToString(); mapArgs["PreviousEngineState"] = previousState.ToString(); FillEventArgs(mapArgs, logContext); EventInstance entry = new EventInstance(eventId, EngineLifecycleCategoryId); entry.EntryType = EventLogEntryType.Information; string detail = GetEventDetail("EngineLifecycleContext", mapArgs); LogEvent(entry, newState, previousState, detail); }
/// <summary> /// DummyLogProvider does nothing to Logging EngineHealthEvent /// </summary> /// <param name="logContext"></param> /// <param name="eventId"></param> /// <param name="exception"></param> /// <param name="additionalInfo"></param> /// internal override void LogEngineHealthEvent(LogContext logContext, int eventId, Exception exception, Dictionary<String, String> additionalInfo) { }
/// <summary> /// Log one pipeline execution detail event. Detail message is already chopped up so that it will /// fit in 64K. /// </summary> /// <param name="logContext"></param> /// <param name="pipelineExecutionDetail"></param> /// <param name="detailSequence"></param> /// <param name="detailTotal"></param> private void LogPipelineExecutionDetailEvent(LogContext logContext, String pipelineExecutionDetail, int detailSequence, int detailTotal) { int eventId = _pipelineExecutionDetailEventId; Hashtable mapArgs = new Hashtable(); mapArgs["PipelineExecutionDetail"] = pipelineExecutionDetail; mapArgs["DetailSequence"] = detailSequence; mapArgs["DetailTotal"] = detailTotal; FillEventArgs(mapArgs, logContext); EventInstance entry = new EventInstance(eventId, PipelineExecutionDetailCategoryId); entry.EntryType = EventLogEntryType.Information; string pipelineInfo = GetEventDetail("PipelineExecutionDetailContext", mapArgs); LogEvent(entry, logContext.CommandLine, pipelineInfo, pipelineExecutionDetail); }
/// <summary> /// DummyLogProvider does nothing to Logging EngineLifecycleEvent /// </summary> /// <param name="logContext"></param> /// <param name="newState"></param> /// <param name="previousState"></param> /// internal override void LogEngineLifecycleEvent(LogContext logContext, EngineState newState, EngineState previousState) { }
/// <summary> /// Log provider lifecycle event. /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="newState"></param> internal override void LogProviderLifecycleEvent(LogContext logContext, string providerName, ProviderState newState) { int eventId = GetProviderLifecycleEventId(newState); if (eventId == _invalidEventId) return; Hashtable mapArgs = new Hashtable(); mapArgs["ProviderName"] = providerName; mapArgs["NewProviderState"] = newState.ToString(); FillEventArgs(mapArgs, logContext); EventInstance entry = new EventInstance(eventId, ProviderLifecycleCategoryId); entry.EntryType = EventLogEntryType.Information; string detail = GetEventDetail("ProviderLifecycleContext", mapArgs); LogEvent(entry, providerName, newState, detail); }
/// <summary> /// Provider interface function for logging command health event /// </summary> /// <param name="logContext"></param> /// <param name="exception"></param> /// internal override void LogCommandHealthEvent(LogContext logContext, Exception exception) { }
/// <summary> /// Fill event arguments with logContext info. /// /// In EventLog Api, arguments are passed in as an array of objects. /// </summary> /// <param name="mapArgs">An ArrayList to contain the event arguments</param> /// <param name="logContext">The log context containing the info to fill in</param> private static void FillEventArgs(Hashtable mapArgs, LogContext logContext) { mapArgs["Severity"] = logContext.Severity; mapArgs["SequenceNumber"] = logContext.SequenceNumber; mapArgs["HostName"] = logContext.HostName; mapArgs["HostVersion"] = logContext.HostVersion; mapArgs["HostId"] = logContext.HostId; mapArgs["HostApplication"] = logContext.HostApplication; mapArgs["EngineVersion"] = logContext.EngineVersion; mapArgs["RunspaceId"] = logContext.RunspaceId; mapArgs["PipelineId"] = logContext.PipelineId; mapArgs["CommandName"] = logContext.CommandName; mapArgs["CommandType"] = logContext.CommandType; mapArgs["ScriptName"] = logContext.ScriptName; mapArgs["CommandPath"] = logContext.CommandPath; mapArgs["CommandLine"] = logContext.CommandLine; mapArgs["User"] = logContext.User; mapArgs["Time"] = logContext.Time; }
/// <summary> /// DummyLogProvider does nothing to Logging PipelineExecutionDetailEvent. /// </summary> /// <param name="logContext"></param> /// <param name="pipelineExecutionDetail"></param> internal override void LogPipelineExecutionDetailEvent(LogContext logContext, List<String> pipelineExecutionDetail) { }
/// <summary> /// Get Log Provider based on Log Context /// </summary> /// /// <param name="logContext"></param> /// <returns></returns> private static IEnumerable<LogProvider> GetLogProvider(LogContext logContext) { System.Diagnostics.Debug.Assert(logContext != null); System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(logContext.ShellId)); return GetLogProvider(logContext.ShellId); }
/// <summary> /// Provider interface function for logging provider health event /// </summary> /// <param name="logContext"></param> /// <param name="providerName"></param> /// <param name="exception"></param> /// internal override void LogProviderHealthEvent(LogContext logContext, string providerName, Exception exception) { }
/// <summary> /// Generate LogContext structure based on executionContext and invocationInfo passed in. /// /// LogContext structure is used in log provider interface. /// </summary> /// <param name="executionContext"></param> /// <param name="invocationInfo"></param> /// <param name="severity"></param> /// <returns></returns> private static LogContext GetLogContext(ExecutionContext executionContext, InvocationInfo invocationInfo, Severity severity) { if (executionContext == null) return null; LogContext logContext = new LogContext(); string shellId = executionContext.ShellID; logContext.ExecutionContext = executionContext; logContext.ShellId = shellId; logContext.Severity = severity.ToString(); if (executionContext.EngineHostInterface != null) { logContext.HostName = executionContext.EngineHostInterface.Name; logContext.HostVersion = executionContext.EngineHostInterface.Version.ToString(); logContext.HostId = (string)executionContext.EngineHostInterface.InstanceId.ToString(); } logContext.HostApplication = String.Join(" ", Environment.GetCommandLineArgs()); if (executionContext.CurrentRunspace != null) { logContext.EngineVersion = executionContext.CurrentRunspace.Version.ToString(); logContext.RunspaceId = executionContext.CurrentRunspace.InstanceId.ToString(); Pipeline currentPipeline = ((RunspaceBase)executionContext.CurrentRunspace).GetCurrentlyRunningPipeline(); if (currentPipeline != null) { logContext.PipelineId = currentPipeline.InstanceId.ToString(CultureInfo.CurrentCulture); } } logContext.SequenceNumber = NextSequenceNumber; try { if (executionContext.LogContextCache.User == null) { logContext.User = Environment.UserDomainName + "\\" + Environment.UserName; executionContext.LogContextCache.User = logContext.User; } else { logContext.User = executionContext.LogContextCache.User; } } catch (InvalidOperationException) { logContext.User = Logging.UnknownUserName; } System.Management.Automation.Remoting.PSSenderInfo psSenderInfo = executionContext.SessionState.PSVariable.GetValue("PSSenderInfo") as System.Management.Automation.Remoting.PSSenderInfo; if (psSenderInfo != null) { logContext.ConnectedUser = psSenderInfo.UserInfo.Identity.Name; } logContext.Time = DateTime.Now.ToString(CultureInfo.CurrentCulture); if (invocationInfo == null) return logContext; logContext.ScriptName = invocationInfo.ScriptName; logContext.CommandLine = invocationInfo.Line; if (invocationInfo.MyCommand != null) { logContext.CommandName = invocationInfo.MyCommand.Name; logContext.CommandType = invocationInfo.MyCommand.CommandType.ToString(); switch (invocationInfo.MyCommand.CommandType) { case CommandTypes.Application: logContext.CommandPath = ((ApplicationInfo)invocationInfo.MyCommand).Path; break; case CommandTypes.ExternalScript: logContext.CommandPath = ((ExternalScriptInfo)invocationInfo.MyCommand).Path; break; } } return logContext; }
/// <summary> /// DummyLogProvider does nothing to Logging EngineHealthEvent /// </summary> /// <param name="logContext"></param> /// <param name="eventId"></param> /// <param name="exception"></param> /// <param name="additionalInfo"></param> /// internal override void LogEngineHealthEvent(LogContext logContext, int eventId, Exception exception, Dictionary <String, String> additionalInfo) { }