Esempio n. 1
0
        private async Task OnResponseCreated(string id, CancellationToken cancellationToken, Func <EventArgs> getEventData, Func <string, CancellationToken, Func <EventArgs>, Task> signal)
        {
            using (Extensions.NoSynchronizationContext)
            {
                var eventData = EventDataConverter.ConvertFrom(getEventData());
                var response  = eventData?.ResponseMessage as HttpResponseMessage;
                if (response != null)
                {
                    if (response.Headers.Warning != null && response.Headers.Warning.Any())
                    {
                        string warningHeader = response.Headers.Warning.ToString();
                        await signal(Events.Warning, cancellationToken,
                                     () => EventFactory.CreateWarningEvent(warningHeader));
                    }
                    // Log request after response since all our request header are set via middleware pipeline.
                    var request = response?.RequestMessage;
                    if (request != null)
                    {
                        await signal(Events.Debug, cancellationToken,
                                     () => EventFactory.CreateLogEvent(HttpMessageLogFormatter.GetHttpRequestLog(request)));
                    }

                    await signal(Events.Debug, cancellationToken,
                                 () => EventFactory.CreateLogEvent(HttpMessageLogFormatter.GetHttpResponseLog(response)));
                }
            }
        }
Esempio n. 2
0
 private async Task OnCmdletException(string id, CancellationToken cancellationToken, Func <EventArgs> getEventData, Func <string, CancellationToken, Func <EventArgs>, Task> signal, Exception exception)
 {
     using (Extensions.NoSynchronizationContext)
     {
         var eventData = EventDataConverter.ConvertFrom(getEventData());
         await signal(Events.Debug, cancellationToken, () => EventFactory.CreateLogEvent($"[{id}]: Received exception with message '{eventData?.Message}'"));
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Handles the Finally event, which is called just after a response is received.
 /// </summary>
 /// <param name="id">The ID of the event</param>
 /// <param name="cancellationToken">The cancellation token for the event</param>
 /// <param name="getEventData">A delegate to get the detailed event data</param>
 /// <param name="signal">The callback for the event dispatcher</param>
 /// <returns>
 /// A <see cref="global::System.Threading.Tasks.Task" /> that will be complete when handling of the event is completed.
 /// </returns>
 private async Task Finally(string id, CancellationToken cancellationToken, Func <EventArgs> getEventData, Func <string, CancellationToken, Func <EventArgs>, Task> signal)
 {
     using (Extensions.NoSynchronizationContext)
     {
         var eventData = EventDataConverter.ConvertFrom(getEventData());
         using (var responseFormatter = new HttpMessageFormatter(eventData.ResponseMessage as HttpResponseMessage))
         {
             var responseString = await responseFormatter.ReadAsStringAsync();
             await signal(Events.Debug, cancellationToken, () => EventFactory.CreateLogEvent(responseString));
         }
     }
 }
Esempio n. 4
0
 private async Task OnCmdletEndProcessing(string id, CancellationToken cancellationToken, Func <EventArgs> getEventData, Func <string, CancellationToken, Func <EventArgs>, Task> signal, InvocationInfo invocationInfo)
 {
     using (Extensions.NoSynchronizationContext)
     {
         string[] commandNameSegment = invocationInfo.MyCommand.Name.Split('_');
         if (commandNameSegment.Length > 1)
         {
             await signal(Events.Debug, cancellationToken, () => EventFactory.CreateLogEvent($"[{id}]: - {commandNameSegment[0]} end processing."));
         }
         else
         {
             await signal(Events.Debug, cancellationToken, () => EventFactory.CreateLogEvent($"[{id}]: - {invocationInfo.MyCommand.Name} end processing."));
         }
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Print event details to the provided stream
        /// </summary>
        /// <param name="getEventData">The event data to print</param>
        /// <param name="signal">The delegate for signaling events to the runtime</param>
        /// <param name="token">The cancellation token for the request</param>
        /// <param name="streamName">The name of the stream to print data to</param>
        /// <param name="eventName">The name of the event to be printed</param>
        public static async void Print(this Func <EventArgs> getEventData, Func <string, CancellationToken, Func <EventArgs>, Task> signal, CancellationToken token, string streamName, string eventName)
        {
            var eventDisplayName = EventFactory.SplitPascalCase(eventName).ToUpperInvariant();
            var data             = EventDataConverter.ConvertFrom(getEventData()); // also, we manually use our TypeConverter to return an appropriate type

            if (data.Id != Events.Verbose && data.Id != Events.Warning && data.Id != Events.Debug && data.Id != Events.Information && data.Id != Events.Error)
            {
                await signal(streamName, token, () => EventFactory.CreateLogEvent($"{eventDisplayName} The contents are '{data?.Id}' and '{data?.Message}'"));

                if (data != null)
                {
                    await signal(streamName, token, () => EventFactory.CreateLogEvent($"{eventDisplayName} Parameter: '{data.Parameter}'\n{eventDisplayName} RequestMessage '{data.RequestMessage}'\n{eventDisplayName} Response: '{data.ResponseMessage}'\n{eventDisplayName} Value: '{data.Value}'"));
                    await signal(streamName, token, () => EventFactory.CreateLogEvent($"{eventDisplayName} ExtendedData Type: '{data.ExtendedData?.GetType()}'\n{eventDisplayName} ExtendedData '{data.ExtendedData}'"));
                }
            }
        }
Esempio n. 6
0
        private async Task OnCmdletBeginProcessing(string id, CancellationToken cancellationToken, Func <EventArgs> getEventData, Func <string, CancellationToken, Func <EventArgs>, Task> signal, InvocationInfo invocationInfo)
        {
            using (Extensions.NoSynchronizationContext)
            {
                string[] commandNameSegment = invocationInfo.MyCommand.Name.Split('_');
                if (commandNameSegment.Length > 1)
                {
                    await signal(Events.Debug, cancellationToken, () => EventFactory.CreateLogEvent($"[{id}]: - {commandNameSegment[0]} begin processing with parameterSet '{commandNameSegment[1]}'."));
                }
                else
                {
                    await signal(Events.Debug, cancellationToken, () => EventFactory.CreateLogEvent($"[{id}]: - {invocationInfo.MyCommand.Name} begin processing."));
                }
                IAuthContext authContext = Microsoft.Graph.PowerShell.Authentication.GraphSession.Instance.AuthContext;
                if (authContext != null)
                {
                    await signal(Events.Debug, cancellationToken, () => EventFactory.CreateLogEvent($"[Authentication]: - AuthType: '{authContext.AuthType}', AuthProviderType: '{authContext.AuthProviderType}', ContextScope: '{authContext.ContextScope}', AppName: '{authContext.AppName}'."));

                    var scopes = authContext.Scopes == null ? string.Empty : string.Join(", ", authContext.Scopes);
                    await signal(Events.Debug, cancellationToken, () => EventFactory.CreateLogEvent($"[Authentication]: - Scopes: [{scopes}]."));
                }
            }
        }