|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -using System.Collections; |
5 | 4 | using System.Diagnostics;
|
6 | 5 | using System.Diagnostics.CodeAnalysis;
|
7 |
| -using System.Globalization; |
8 | 6 | using System.Runtime.CompilerServices;
|
9 | 7 | using Microsoft.AspNetCore.Http;
|
10 | 8 | using Microsoft.AspNetCore.Http.Features;
|
@@ -97,11 +95,6 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
|
97 | 95 | // To avoid allocation, return a null scope if the logger is not on at least to some degree.
|
98 | 96 | if (loggingEnabled)
|
99 | 97 | {
|
100 |
| - // Scope may be relevant for a different level of logging, so we always create it |
101 |
| - // see: https://github.com/aspnet/Hosting/pull/944 |
102 |
| - // Scope can be null if logging is not on. |
103 |
| - context.Scope = Log.RequestScope(_logger, httpContext); |
104 |
| - |
105 | 98 | if (_logger.IsEnabled(LogLevel.Information))
|
106 | 99 | {
|
107 | 100 | if (startTimestamp == 0)
|
@@ -215,9 +208,6 @@ public void RequestEnd(HttpContext httpContext, Exception? exception, HostingApp
|
215 | 208 | _eventSource.RequestFailed();
|
216 | 209 | }
|
217 | 210 | }
|
218 |
| - |
219 |
| - // Logging Scope is finshed with |
220 |
| - context.Scope?.Dispose(); |
221 | 211 | }
|
222 | 212 |
|
223 | 213 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
@@ -506,74 +496,4 @@ private void StopActivity(Activity activity, HttpContext httpContext)
|
506 | 496 | WriteDiagnosticEvent(_diagnosticListener, ActivityStopKey, httpContext);
|
507 | 497 | activity.Stop(); // Resets Activity.Current (we want this after the Write)
|
508 | 498 | }
|
509 |
| - |
510 |
| - private static class Log |
511 |
| - { |
512 |
| - public static IDisposable? RequestScope(ILogger logger, HttpContext httpContext) |
513 |
| - { |
514 |
| - return logger.BeginScope(new HostingLogScope(httpContext)); |
515 |
| - } |
516 |
| - |
517 |
| - private sealed class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>> |
518 |
| - { |
519 |
| - private readonly string _path; |
520 |
| - private readonly string _traceIdentifier; |
521 |
| - |
522 |
| - private string? _cachedToString; |
523 |
| - |
524 |
| - public int Count => 2; |
525 |
| - |
526 |
| - public KeyValuePair<string, object> this[int index] |
527 |
| - { |
528 |
| - get |
529 |
| - { |
530 |
| - if (index == 0) |
531 |
| - { |
532 |
| - return new KeyValuePair<string, object>("RequestId", _traceIdentifier); |
533 |
| - } |
534 |
| - else if (index == 1) |
535 |
| - { |
536 |
| - return new KeyValuePair<string, object>("RequestPath", _path); |
537 |
| - } |
538 |
| - |
539 |
| - throw new ArgumentOutOfRangeException(nameof(index)); |
540 |
| - } |
541 |
| - } |
542 |
| - |
543 |
| - public HostingLogScope(HttpContext httpContext) |
544 |
| - { |
545 |
| - _traceIdentifier = httpContext.TraceIdentifier; |
546 |
| - _path = (httpContext.Request.PathBase.HasValue |
547 |
| - ? httpContext.Request.PathBase + httpContext.Request.Path |
548 |
| - : httpContext.Request.Path).ToString(); |
549 |
| - } |
550 |
| - |
551 |
| - public override string ToString() |
552 |
| - { |
553 |
| - if (_cachedToString == null) |
554 |
| - { |
555 |
| - _cachedToString = string.Format( |
556 |
| - CultureInfo.InvariantCulture, |
557 |
| - "RequestPath:{0} RequestId:{1}", |
558 |
| - _path, |
559 |
| - _traceIdentifier); |
560 |
| - } |
561 |
| - |
562 |
| - return _cachedToString; |
563 |
| - } |
564 |
| - |
565 |
| - public IEnumerator<KeyValuePair<string, object>> GetEnumerator() |
566 |
| - { |
567 |
| - for (var i = 0; i < Count; ++i) |
568 |
| - { |
569 |
| - yield return this[i]; |
570 |
| - } |
571 |
| - } |
572 |
| - |
573 |
| - IEnumerator IEnumerable.GetEnumerator() |
574 |
| - { |
575 |
| - return GetEnumerator(); |
576 |
| - } |
577 |
| - } |
578 |
| - } |
579 | 499 | }
|
0 commit comments