diff --git a/src/Microsoft.AspNet.Hosting/Internal/FastHttpRequestIdentifierFeature.cs b/src/Microsoft.AspNet.Hosting/Internal/FastHttpRequestIdentifierFeature.cs deleted file mode 100644 index 95e78065..00000000 --- a/src/Microsoft.AspNet.Hosting/Internal/FastHttpRequestIdentifierFeature.cs +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Threading; -using Microsoft.AspNet.Http.Features; - -namespace Microsoft.AspNet.Hosting.Internal -{ - public class FastHttpRequestIdentifierFeature : IHttpRequestIdentifierFeature - { - // Base64 encoding - but in ascii sort order for easy text based sorting - private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; - // Seed the _requestId for this application instance with - // the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 - // for a roughly increasing _requestId over restarts - private static long _requestId = DateTime.UtcNow.Ticks; - - private string _id = null; - - public string TraceIdentifier - { - get - { - // Don't incur the cost of generating the request ID until it's asked for - if (_id == null) - { - _id = GenerateRequestId(Interlocked.Increment(ref _requestId)); - } - return _id; - } - set - { - _id = value; - } - } - - private static unsafe string GenerateRequestId(long id) - { - // The following routine is ~310% faster than calling long.ToString() on x64 - // and ~600% faster than calling long.ToString() on x86 in tight loops of 1 million+ iterations - // See: https://github.com/aspnet/Hosting/pull/385 - - // stackalloc to allocate array on stack rather than heap - char* charBuffer = stackalloc char[13]; - - charBuffer[0] = _encode32Chars[(int)(id >> 60) & 31]; - charBuffer[1] = _encode32Chars[(int)(id >> 55) & 31]; - charBuffer[2] = _encode32Chars[(int)(id >> 50) & 31]; - charBuffer[3] = _encode32Chars[(int)(id >> 45) & 31]; - charBuffer[4] = _encode32Chars[(int)(id >> 40) & 31]; - charBuffer[5] = _encode32Chars[(int)(id >> 35) & 31]; - charBuffer[6] = _encode32Chars[(int)(id >> 30) & 31]; - charBuffer[7] = _encode32Chars[(int)(id >> 25) & 31]; - charBuffer[8] = _encode32Chars[(int)(id >> 20) & 31]; - charBuffer[9] = _encode32Chars[(int)(id >> 15) & 31]; - charBuffer[10] = _encode32Chars[(int)(id >> 10) & 31]; - charBuffer[11] = _encode32Chars[(int)(id >> 5) & 31]; - charBuffer[12] = _encode32Chars[(int)id & 31]; - - // string ctor overload that takes char* - return new string(charBuffer, 0, 13); - } - } -} diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index d0a95184..2a06116c 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -96,7 +96,6 @@ public virtual IApplication Start() { var httpContext = contextFactory.CreateHttpContext(features); httpContext.ApplicationServices = _applicationServices; - var requestIdentifier = GetRequestIdentifier(httpContext); contextAccessor.HttpContext = httpContext; #pragma warning disable 0618 if (telemetrySource.IsEnabled("Microsoft.AspNet.Hosting.BeginRequest")) @@ -107,7 +106,7 @@ public virtual IApplication Start() try { using (logger.IsEnabled(LogLevel.Critical) - ? logger.BeginScope("Request Id: {RequestId}", requestIdentifier) + ? logger.BeginScope("Request Id: {RequestId}", httpContext.TraceIdentifier) : null) { await application(httpContext); @@ -276,18 +275,6 @@ private void EnsureServer() } } - private string GetRequestIdentifier(HttpContext httpContext) - { - var requestIdentifierFeature = httpContext.Features.Get(); - if (requestIdentifierFeature == null) - { - requestIdentifierFeature = new FastHttpRequestIdentifierFeature(); - httpContext.Features.Set(requestIdentifierFeature); - } - - return requestIdentifierFeature.TraceIdentifier; - } - private class Disposable : IDisposable { private Action _dispose; diff --git a/src/Microsoft.AspNet.Hosting/project.json b/src/Microsoft.AspNet.Hosting/project.json index 18ea1162..a6e69bdb 100644 --- a/src/Microsoft.AspNet.Hosting/project.json +++ b/src/Microsoft.AspNet.Hosting/project.json @@ -6,8 +6,7 @@ "url": "git://github.com/aspnet/hosting" }, "compilationOptions": { - "warningsAsErrors": true, - "allowUnsafe": true + "warningsAsErrors": true }, "dependencies": { "Microsoft.AspNet.FileProviders.Physical": "1.0.0-*", diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index 159c2775..168f908e 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -305,7 +305,13 @@ public void HostingEngine_CreatesDefaultRequestIdentifierFeature_IfNotPresent() // Assert Assert.NotNull(httpContext); - Assert.IsType(httpContext.Features.Get()); + Assert.Null(httpContext.Features.Get()); + + Assert.NotNull(httpContext.TraceIdentifier); + Assert.NotNull(httpContext.Features.Get()); + Assert.IsType(httpContext.Features.Get()); + + Assert.Same(httpContext.TraceIdentifier, httpContext.Features.Get().TraceIdentifier); } [Fact] diff --git a/test/Microsoft.AspNet.Hosting.Tests/Internal/FastHttpRequestIdentifierFeatureTests.cs b/test/Microsoft.AspNet.Hosting.Tests/Internal/FastHttpRequestIdentifierFeatureTests.cs deleted file mode 100644 index fff0fb60..00000000 --- a/test/Microsoft.AspNet.Hosting.Tests/Internal/FastHttpRequestIdentifierFeatureTests.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.AspNet.Hosting.Internal; -using Xunit; - -namespace Microsoft.AspNet.Hosting.Tests.Internal -{ - public class FastHttpRequestIdentifierFeatureTests - { - [Fact] - public void TraceIdentifier_ReturnsId() - { - var feature = new FastHttpRequestIdentifierFeature(); - - var id = feature.TraceIdentifier; - - Assert.NotNull(id); - } - - [Fact] - public void TraceIdentifier_ReturnsStableId() - { - var feature = new FastHttpRequestIdentifierFeature(); - - var id1 = feature.TraceIdentifier; - var id2 = feature.TraceIdentifier; - - Assert.Equal(id1, id2); - } - - [Fact] - public void TraceIdentifier_ReturnsUniqueIdForDifferentInstances() - { - var feature1 = new FastHttpRequestIdentifierFeature(); - var feature2 = new FastHttpRequestIdentifierFeature(); - - var id1 = feature1.TraceIdentifier; - var id2 = feature2.TraceIdentifier; - - Assert.NotEqual(id1, id2); - } - } -}