@@ -56,19 +56,25 @@ partial void CustomInit()
5656 /// </returns>
5757 public async Task EventHandler ( string id , CancellationToken cancellationToken , Func < EventArgs > getEventData , Func < string , CancellationToken , Func < EventArgs > , Task > signal , InvocationInfo invocationInfo , string parameterSetName , System . Exception exception )
5858 {
59- switch ( id )
59+ if ( invocationInfo . BoundParameters . ContainsKey ( "Debug" ) )
6060 {
61- case Events . Finally :
62- await Finally ( id , cancellationToken , getEventData , signal ) ;
63- break ;
64- default :
65- getEventData . Print ( signal , cancellationToken , Events . Information , id ) ;
66- break ;
61+ switch ( id )
62+ {
63+ case Events . BeforeCall :
64+ await BeforeCall ( id , cancellationToken , getEventData , signal ) ;
65+ break ;
66+ case Events . Finally :
67+ await Finally ( id , cancellationToken , getEventData , signal ) ;
68+ break ;
69+ default :
70+ getEventData . Print ( signal , cancellationToken , Events . Information , id ) ;
71+ break ;
72+ }
6773 }
6874 }
6975
7076 /// <summary>
71- /// Handles the Finally event, which is called just before Request and Response are disposed .
77+ /// Handles the Finally event, which is called just after a response is received .
7278 /// </summary>
7379 /// <param name="id">The ID of the event</param>
7480 /// <param name="cancellationToken">The cancellation token for the event</param>
@@ -82,15 +88,35 @@ private async Task Finally(string id, CancellationToken cancellationToken, Func<
8288 using ( Extensions . NoSynchronizationContext )
8389 {
8490 var eventData = EventDataConverter . ConvertFrom ( getEventData ( ) ) ;
85- using ( var requestFormatter = new HttpMessageFormatter ( eventData . RequestMessage as HttpRequestMessage ) )
8691 using ( var responseFormatter = new HttpMessageFormatter ( eventData . ResponseMessage as HttpResponseMessage ) )
8792 {
88- var requestString = await requestFormatter . ReadAsStringAsync ( ) ;
8993 var responseString = await responseFormatter . ReadAsStringAsync ( ) ;
90- await signal ( Events . Debug , cancellationToken , ( ) => EventFactory . CreateLogEvent ( requestString ) ) ;
9194 await signal ( Events . Debug , cancellationToken , ( ) => EventFactory . CreateLogEvent ( responseString ) ) ;
9295 }
9396 }
9497 }
98+
99+ /// <summary>
100+ /// Handles the BeforeCall event, which is called just before Request is sent.
101+ /// </summary>
102+ /// <param name="id">The ID of the event</param>
103+ /// <param name="cancellationToken">The cancellation token for the event</param>
104+ /// <param name="getEventData">A delegate to get the detailed event data</param>
105+ /// <param name="signal">The callback for the event dispatcher</param>
106+ /// <returns>
107+ /// A <see cref="global::System.Threading.Tasks.Task" /> that will be complete when handling of the event is completed.
108+ /// </returns>
109+ private async Task BeforeCall ( string id , CancellationToken cancellationToken , Func < EventArgs > getEventData , Func < string , CancellationToken , Func < EventArgs > , Task > signal )
110+ {
111+ using ( Extensions . NoSynchronizationContext )
112+ {
113+ var eventData = EventDataConverter . ConvertFrom ( getEventData ( ) ) ;
114+ using ( var requestFormatter = new HttpMessageFormatter ( eventData . RequestMessage as HttpRequestMessage ) )
115+ {
116+ var requestString = await requestFormatter . ReadAsStringAsync ( ) ;
117+ await signal ( Events . Debug , cancellationToken , ( ) => EventFactory . CreateLogEvent ( requestString ) ) ;
118+ }
119+ }
120+ }
95121 }
96122}
0 commit comments