Skip to content
This repository was archived by the owner on Mar 19, 2019. It is now read-only.

Commit 58af4c7

Browse files
committed
React to Logging API changes
1 parent 1cae069 commit 58af4c7

File tree

3 files changed

+95
-21
lines changed

3 files changed

+95
-21
lines changed

Diff for: src/Microsoft.Net.Http.Server/LogHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public bool IsEnabled(LogLevel logLevel)
9999
return false;
100100
}
101101

102-
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
102+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
103103
{
104104
}
105105

Diff for: src/Microsoft.Net.Http.Server/RequestProcessing/Request.cs

+50-11
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ internal unsafe Request(RequestContext httpContext, NativeRequestContext memoryB
153153

154154
if (_requestContext.Logger.IsEnabled(LogLevel.Debug))
155155
{
156-
RequestContext.Logger.LogDebug(new ReceiveRequestLogContext(this));
156+
RequestContext.Logger.Log(LogLevel.Debug, eventId: 0, state: new ReceiveRequestLogContext(this), exception: null, formatter: ReceiveRequestLogContext.Callback);
157157
}
158158
}
159159

@@ -534,8 +534,10 @@ internal void SwitchToOpaqueMode()
534534
}
535535
}
536536

537-
private class ReceiveRequestLogContext : ILogValues
537+
private class ReceiveRequestLogContext : IReadOnlyList<KeyValuePair<string, object>>
538538
{
539+
internal static readonly Func<object, Exception, string> Callback = (state, exception) => ((ReceiveRequestLogContext)state).ToString();
540+
539541
private readonly Request _request;
540542

541543
internal ReceiveRequestLogContext(Request request)
@@ -550,17 +552,36 @@ internal ReceiveRequestLogContext(Request request)
550552
public string Protocol { get { return "HTTP/" + _request.ProtocolVersion.ToString(2); } }
551553
public IEnumerable Headers { get { return new HeadersLogStructure(_request.Headers); } }
552554

553-
public IEnumerable<KeyValuePair<string, object>> GetValues()
555+
public int Count
554556
{
555-
return new[]
557+
get
556558
{
557-
new KeyValuePair<string, object>("Method", Method),
558-
new KeyValuePair<string, object>("PathBase", PathBase),
559-
new KeyValuePair<string, object>("Path", Path),
560-
new KeyValuePair<string, object>("Query", Query),
561-
new KeyValuePair<string, object>("Protocol", Protocol),
562-
new KeyValuePair<string, object>("Headers", Headers),
563-
};
559+
return 6;
560+
}
561+
}
562+
563+
public KeyValuePair<string, object> this[int index]
564+
{
565+
get
566+
{
567+
switch (index)
568+
{
569+
case 0:
570+
return new KeyValuePair<string, object>("Method", Method);
571+
case 1:
572+
return new KeyValuePair<string, object>("PathBase", PathBase);
573+
case 2:
574+
return new KeyValuePair<string, object>("Path", Path);
575+
case 3:
576+
return new KeyValuePair<string, object>("Query", Query);
577+
case 4:
578+
return new KeyValuePair<string, object>("Protocol", Protocol);
579+
case 5:
580+
return new KeyValuePair<string, object>("Headers", Headers);
581+
default:
582+
throw new IndexOutOfRangeException(nameof(index));
583+
}
584+
}
564585
}
565586

566587
public override string ToString()
@@ -590,6 +611,24 @@ public override string ToString()
590611
requestBuilder.Append("}");
591612
return requestBuilder.ToString();
592613
}
614+
615+
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
616+
{
617+
return new List<KeyValuePair<string, object>>
618+
{
619+
new KeyValuePair<string, object>("Method", Method),
620+
new KeyValuePair<string, object>("PathBase", PathBase),
621+
new KeyValuePair<string, object>("Path", Path),
622+
new KeyValuePair<string, object>("Query", Query),
623+
new KeyValuePair<string, object>("Protocol", Protocol),
624+
new KeyValuePair<string, object>("Headers", Headers)
625+
}.GetEnumerator();
626+
}
627+
628+
IEnumerator IEnumerable.GetEnumerator()
629+
{
630+
return GetEnumerator();
631+
}
593632
}
594633
}
595634
}

Diff for: src/Microsoft.Net.Http.Server/RequestProcessing/Response.cs

+44-9
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ internal unsafe uint SendHeaders(HttpApi.HTTP_DATA_CHUNK[] dataChunks,
358358

359359
if (RequestContext.Logger.IsEnabled(LogLevel.Debug))
360360
{
361-
RequestContext.Logger.LogDebug(new SendResponseLogContext(this));
361+
RequestContext.Logger.Log(LogLevel.Debug, eventId: 0, state: new SendResponseLogContext(this), exception: null, formatter: SendResponseLogContext.Callback);
362362
}
363363

364364
/*
@@ -824,8 +824,10 @@ private void NotifyOnResponseCompleted()
824824
}
825825
}
826826

827-
private class SendResponseLogContext : ILogValues
827+
private class SendResponseLogContext : IReadOnlyList<KeyValuePair<string, object>>
828828
{
829+
internal static readonly Func<object, Exception, string> Callback = (state, exception) => ((SendResponseLogContext)state).ToString();
830+
829831
private readonly Response _response;
830832

831833
internal SendResponseLogContext(Response response)
@@ -838,15 +840,32 @@ internal SendResponseLogContext(Response response)
838840
public string ReasonPhrase { get { return _response.ReasonPhrase ?? _response.GetReasonPhrase(_response.StatusCode); } }
839841
public IEnumerable Headers { get { return new HeadersLogStructure(_response.Headers); } }
840842

841-
public IEnumerable<KeyValuePair<string, object>> GetValues()
843+
public int Count
842844
{
843-
return new[]
845+
get
844846
{
845-
new KeyValuePair<string, object>("Protocol", Protocol),
846-
new KeyValuePair<string, object>("StatusCode", StatusCode),
847-
new KeyValuePair<string, object>("ReasonPhrase", ReasonPhrase),
848-
new KeyValuePair<string, object>("Headers", Headers),
849-
};
847+
return 4;
848+
}
849+
}
850+
851+
public KeyValuePair<string, object> this[int index]
852+
{
853+
get
854+
{
855+
switch (index)
856+
{
857+
case 0:
858+
return new KeyValuePair<string, object>("Protocol", Protocol);
859+
case 1:
860+
return new KeyValuePair<string, object>("StatusCode", StatusCode);
861+
case 2:
862+
return new KeyValuePair<string, object>("ReasonPhrase", ReasonPhrase);
863+
case 3:
864+
return new KeyValuePair<string, object>("Headers", Headers);
865+
default:
866+
throw new IndexOutOfRangeException(nameof(index));
867+
}
868+
}
850869
}
851870

852871
public override string ToString()
@@ -873,6 +892,22 @@ public override string ToString()
873892
responseBuilder.Append("}");
874893
return responseBuilder.ToString();
875894
}
895+
896+
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
897+
{
898+
return new List<KeyValuePair<string, object>>
899+
{
900+
new KeyValuePair<string, object>("Protocol", Protocol),
901+
new KeyValuePair<string, object>("StatusCode", StatusCode),
902+
new KeyValuePair<string, object>("ReasonPhrase", ReasonPhrase),
903+
new KeyValuePair<string, object>("Headers", Headers)
904+
}.GetEnumerator();
905+
}
906+
907+
IEnumerator IEnumerable.GetEnumerator()
908+
{
909+
return GetEnumerator();
910+
}
876911
}
877912
}
878913
}

0 commit comments

Comments
 (0)