Remove HttpSys LogHelper #18029 (#18030)

This commit is contained in:
Kahbazi 2020-01-02 20:41:13 +03:30 committed by Chris Ross
parent adb12f23d0
commit c7937640a4
10 changed files with 64 additions and 181 deletions

View File

@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
Options = options; Options = options;
Logger = LogHelper.CreateLogger(loggerFactory, typeof(HttpSysListener)); Logger = loggerFactory.CreateLogger<HttpSysListener>();
_state = State.Stopped; _state = State.Stopped;
_internalLock = new object(); _internalLock = new object();
@ -92,7 +92,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
_requestQueue?.Dispose(); _requestQueue?.Dispose();
_urlGroup?.Dispose(); _urlGroup?.Dispose();
_serverSession?.Dispose(); _serverSession?.Dispose();
LogHelper.LogException(Logger, ".Ctor", exception); Logger.LogError(0, exception, ".Ctor");
throw; throw;
} }
} }
@ -135,7 +135,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
CheckDisposed(); CheckDisposed();
LogHelper.LogTrace(Logger, "Starting the listener."); Logger.LogTrace("Starting the listener.");
// Make sure there are no race conditions between Start/Stop/Abort/Close/Dispose. // Make sure there are no race conditions between Start/Stop/Abort/Close/Dispose.
// Start needs to setup all resources. Abort/Stop must not interfere while Start is // Start needs to setup all resources. Abort/Stop must not interfere while Start is
@ -177,7 +177,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
// Make sure the HttpListener instance can't be used if Start() failed. // Make sure the HttpListener instance can't be used if Start() failed.
_state = State.Disposed; _state = State.Disposed;
DisposeInternal(); DisposeInternal();
LogHelper.LogException(Logger, "Start", exception); Logger.LogError(0, exception, "Start");
throw; throw;
} }
} }
@ -195,7 +195,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
return; return;
} }
LogHelper.LogTrace(Logger, "Stopping the listener."); Logger.LogTrace("Stopping the listener.");
// If this instance created the queue then remove the URL prefixes before shutting down. // If this instance created the queue then remove the URL prefixes before shutting down.
if (_requestQueue.Created) if (_requestQueue.Created)
@ -210,7 +210,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Exception exception) catch (Exception exception)
{ {
LogHelper.LogException(Logger, "Stop", exception); Logger.LogError(0, exception, "Stop");
throw; throw;
} }
} }
@ -238,14 +238,14 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
return; return;
} }
LogHelper.LogTrace(Logger, "Disposing the listener."); Logger.LogTrace("Disposing the listener.");
Stop(); Stop();
DisposeInternal(); DisposeInternal();
} }
catch (Exception exception) catch (Exception exception)
{ {
LogHelper.LogException(Logger, "Dispose", exception); Logger.LogError(0, exception, "Dispose");
throw; throw;
} }
finally finally
@ -300,7 +300,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Exception exception) catch (Exception exception)
{ {
LogHelper.LogException(Logger, "GetContextAsync", exception); Logger.LogError(0, exception, "GetContextAsync");
throw; throw;
} }

View File

@ -1,118 +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.Diagnostics;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Server.HttpSys
{
internal static class LogHelper
{
internal static ILogger CreateLogger(ILoggerFactory factory, Type type)
{
if (factory == null)
{
return null;
}
return factory.CreateLogger(type.FullName);
}
internal static void LogInfo(ILogger logger, string data)
{
if (logger == null)
{
Debug.WriteLine(data);
}
else
{
logger.LogInformation(data);
}
}
internal static void LogWarning(ILogger logger, string data)
{
if (logger == null)
{
Debug.WriteLine(data);
}
else
{
logger.LogWarning(data);
}
}
internal static void LogDebug(ILogger logger, string data)
{
if (logger == null)
{
Debug.WriteLine(data);
}
else
{
logger.LogDebug(data);
}
}
internal static void LogDebug(ILogger logger, string location, string data)
{
if (logger == null)
{
Debug.WriteLine(data);
}
else
{
logger.LogDebug(location + "; " + data);
}
}
internal static void LogDebug(ILogger logger, string location, Exception exception)
{
if (logger == null)
{
Debug.WriteLine(location + Environment.NewLine + exception.ToString());
}
else
{
logger.LogDebug(0, exception, location);
}
}
internal static void LogTrace(ILogger logger, string data)
{
if (logger == null)
{
Debug.WriteLine(data);
}
else
{
logger.LogTrace(data);
}
}
internal static void LogException(ILogger logger, string location, Exception exception)
{
if (logger == null)
{
Debug.WriteLine(location + Environment.NewLine + exception.ToString());
}
else
{
logger.LogError(0, exception, location);
}
}
internal static void LogError(ILogger logger, string location, string message)
{
if (logger == null)
{
Debug.WriteLine(message);
}
else
{
logger.LogError(location + "; " + message);
}
}
}
}

View File

@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
_options = options.Value; _options = options.Value;
Listener = new HttpSysListener(_options, loggerFactory); Listener = new HttpSysListener(_options, loggerFactory);
_logger = LogHelper.CreateLogger(loggerFactory, typeof(MessagePump)); _logger = loggerFactory.CreateLogger<MessagePump>();
if (_options.Authentication.Schemes != AuthenticationSchemes.None) if (_options.Authentication.Schemes != AuthenticationSchemes.None)
{ {
@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
if (_options.UrlPrefixes.Count > 0) if (_options.UrlPrefixes.Count > 0)
{ {
LogHelper.LogWarning(_logger, $"Overriding endpoints added to {nameof(HttpSysOptions.UrlPrefixes)} since {nameof(IServerAddressesFeature.PreferHostingUrls)} is set to true." + _logger.LogWarning($"Overriding endpoints added to {nameof(HttpSysOptions.UrlPrefixes)} since {nameof(IServerAddressesFeature.PreferHostingUrls)} is set to true." +
$" Binding to address(es) '{string.Join(", ", _serverAddresses.Addresses)}' instead. "); $" Binding to address(es) '{string.Join(", ", _serverAddresses.Addresses)}' instead. ");
Listener.Options.UrlPrefixes.Clear(); Listener.Options.UrlPrefixes.Clear();
@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
if (hostingUrlsPresent) if (hostingUrlsPresent)
{ {
LogHelper.LogWarning(_logger, $"Overriding address(es) '{string.Join(", ", _serverAddresses.Addresses)}'. " + _logger.LogWarning($"Overriding address(es) '{string.Join(", ", _serverAddresses.Addresses)}'. " +
$"Binding to endpoints added to {nameof(HttpSysOptions.UrlPrefixes)} instead."); $"Binding to endpoints added to {nameof(HttpSysOptions.UrlPrefixes)} instead.");
_serverAddresses.Addresses.Clear(); _serverAddresses.Addresses.Clear();
@ -108,7 +108,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
else if (Listener.RequestQueue.Created) else if (Listener.RequestQueue.Created)
{ {
LogHelper.LogDebug(_logger, $"No listening endpoints were configured. Binding to {Constants.DefaultServerAddress} by default."); _logger.LogDebug($"No listening endpoints were configured. Binding to {Constants.DefaultServerAddress} by default.");
Listener.Options.UrlPrefixes.Add(Constants.DefaultServerAddress); Listener.Options.UrlPrefixes.Add(Constants.DefaultServerAddress);
} }
@ -171,11 +171,11 @@ namespace Microsoft.AspNetCore.Server.HttpSys
Contract.Assert(Stopping); Contract.Assert(Stopping);
if (Stopping) if (Stopping)
{ {
LogHelper.LogDebug(_logger, "ListenForNextRequestAsync-Stopping", exception); _logger.LogDebug(0, exception, "ListenForNextRequestAsync-Stopping");
} }
else else
{ {
LogHelper.LogException(_logger, "ListenForNextRequestAsync", exception); _logger.LogError(0, exception, "ListenForNextRequestAsync");
} }
continue; continue;
} }
@ -187,7 +187,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
// Request processing failed to be queued in threadpool // Request processing failed to be queued in threadpool
// Log the error message, release throttle and move on // Log the error message, release throttle and move on
LogHelper.LogException(_logger, "ProcessRequestAsync", ex); _logger.LogError(0, ex, "ProcessRequestAsync");
} }
} }
Interlocked.Decrement(ref _acceptorCounts); Interlocked.Decrement(ref _acceptorCounts);
@ -224,7 +224,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Exception ex) catch (Exception ex)
{ {
LogHelper.LogException(_logger, "ProcessRequestAsync", ex); _logger.LogError(0, ex, "ProcessRequestAsync");
_application.DisposeContext(context, ex); _application.DisposeContext(context, ex);
if (requestContext.Response.HasStarted) if (requestContext.Response.HasStarted)
{ {
@ -247,14 +247,14 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
if (Interlocked.Decrement(ref _outstandingRequests) == 0 && Stopping) if (Interlocked.Decrement(ref _outstandingRequests) == 0 && Stopping)
{ {
LogHelper.LogInfo(_logger, "All requests drained."); _logger.LogInformation("All requests drained.");
_shutdownSignal.TrySetResult(0); _shutdownSignal.TrySetResult(0);
} }
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
LogHelper.LogException(_logger, "ProcessRequestAsync", ex); _logger.LogError(0, ex, "ProcessRequestAsync");
requestContext.Abort(); requestContext.Abort();
} }
} }
@ -274,7 +274,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
if (Interlocked.Exchange(ref _shutdownSignalCompleted, 1) == 0) if (Interlocked.Exchange(ref _shutdownSignalCompleted, 1) == 0)
{ {
LogHelper.LogInfo(_logger, "Canceled, terminating " + _outstandingRequests + " request(s)."); _logger.LogInformation("Canceled, terminating " + _outstandingRequests + " request(s).");
_shutdownSignal.TrySetResult(null); _shutdownSignal.TrySetResult(null);
} }
}); });
@ -292,7 +292,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
// Wait for active requests to drain // Wait for active requests to drain
if (_outstandingRequests > 0) if (_outstandingRequests > 0)
{ {
LogHelper.LogInfo(_logger, "Stopping, waiting for " + _outstandingRequests + " request(s) to drain."); _logger.LogInformation("Stopping, waiting for " + _outstandingRequests + " request(s) to drain.");
RegisterCancelation(); RegisterCancelation();
} }
else else

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Win32Exception exception) catch (Win32Exception exception)
{ {
LogHelper.LogException(_logger, "GetConnectionToken", exception); _logger.LogError(0, exception, "GetConnectionToken");
return CancellationToken.None; return CancellationToken.None;
} }
} }
@ -54,12 +54,12 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
// Race condition on creation has no side effects // Race condition on creation has no side effects
var cancellation = new ConnectionCancellation(this); var cancellation = new ConnectionCancellation(this);
return _connectionCancellationTokens.GetOrAdd(connectionId, cancellation); return _connectionCancellationTokens.GetOrAdd(connectionId, cancellation);
} }
private unsafe CancellationToken CreateDisconnectToken(ulong connectionId) private unsafe CancellationToken CreateDisconnectToken(ulong connectionId)
{ {
LogHelper.LogDebug(_logger, "CreateDisconnectToken", "Registering connection for disconnect for connection ID: " + connectionId); _logger.LogDebug("CreateDisconnectToken; Registering connection for disconnect for connection ID: " + connectionId);
// Create a nativeOverlapped callback so we can register for disconnect callback // Create a nativeOverlapped callback so we can register for disconnect callback
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
@ -70,8 +70,8 @@ namespace Microsoft.AspNetCore.Server.HttpSys
nativeOverlapped = new SafeNativeOverlapped(boundHandle, boundHandle.AllocateNativeOverlapped( nativeOverlapped = new SafeNativeOverlapped(boundHandle, boundHandle.AllocateNativeOverlapped(
(errorCode, numBytes, overlappedPtr) => (errorCode, numBytes, overlappedPtr) =>
{ {
LogHelper.LogDebug(_logger, "CreateDisconnectToken", "http.sys disconnect callback fired for connection ID: " + connectionId); _logger.LogDebug("CreateDisconnectToken; http.sys disconnect callback fired for connection ID: " + connectionId);
// Free the overlapped // Free the overlapped
nativeOverlapped.Dispose(); nativeOverlapped.Dispose();
@ -84,7 +84,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (AggregateException exception) catch (AggregateException exception)
{ {
LogHelper.LogException(_logger, "CreateDisconnectToken Callback", exception); _logger.LogError(0, exception, "CreateDisconnectToken Callback");
} }
}, },
null, null)); null, null));
@ -98,7 +98,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
catch (Win32Exception exception) catch (Win32Exception exception)
{ {
statusCode = (uint)exception.NativeErrorCode; statusCode = (uint)exception.NativeErrorCode;
LogHelper.LogException(_logger, "CreateDisconnectToken", exception); _logger.LogError(0, exception, "CreateDisconnectToken");
} }
if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_IO_PENDING && if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_IO_PENDING &&
@ -108,7 +108,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
nativeOverlapped.Dispose(); nativeOverlapped.Dispose();
ConnectionCancellation ignored; ConnectionCancellation ignored;
_connectionCancellationTokens.TryRemove(connectionId, out ignored); _connectionCancellationTokens.TryRemove(connectionId, out ignored);
LogHelper.LogDebug(_logger, "HttpWaitForDisconnectEx", new Win32Exception((int)statusCode)); _logger.LogDebug(0, new Win32Exception((int)statusCode), "HttpWaitForDisconnectEx");
cts.Cancel(); cts.Cancel();
} }

View File

@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
internal void SetProperty(HttpApiTypes.HTTP_SERVER_PROPERTY property, IntPtr info, uint infosize, bool throwOnError = true) internal void SetProperty(HttpApiTypes.HTTP_SERVER_PROPERTY property, IntPtr info, uint infosize, bool throwOnError = true)
{ {
Debug.Assert(info != IntPtr.Zero, "SetUrlGroupProperty called with invalid pointer"); Debug.Assert(info != IntPtr.Zero, "SetUrlGroupProperty called with invalid pointer");
CheckDisposed(); CheckDisposed();
@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS) if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS)
{ {
var exception = new HttpSysException((int)statusCode); var exception = new HttpSysException((int)statusCode);
LogHelper.LogException(_logger, "SetUrlGroupProperty", exception); _logger.LogError(0, exception, "SetUrlGroupProperty");
if (throwOnError) if (throwOnError)
{ {
throw exception; throw exception;
@ -71,7 +71,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
internal void RegisterPrefix(string uriPrefix, int contextId) internal void RegisterPrefix(string uriPrefix, int contextId)
{ {
LogHelper.LogDebug(_logger, "Listening on prefix: " + uriPrefix); _logger.LogDebug("Listening on prefix: " + uriPrefix);
CheckDisposed(); CheckDisposed();
var statusCode = HttpApi.HttpAddUrlToUrlGroup(Id, uriPrefix, (ulong)contextId, 0); var statusCode = HttpApi.HttpAddUrlToUrlGroup(Id, uriPrefix, (ulong)contextId, 0);
@ -87,10 +87,10 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
} }
} }
internal bool UnregisterPrefix(string uriPrefix) internal bool UnregisterPrefix(string uriPrefix)
{ {
LogHelper.LogDebug(_logger, "Stop listening on prefix: " + uriPrefix); _logger.LogInformation("Stop listening on prefix: " + uriPrefix);
CheckDisposed(); CheckDisposed();
var statusCode = HttpApi.HttpRemoveUrlFromUrlGroup(Id, uriPrefix, 0); var statusCode = HttpApi.HttpRemoveUrlFromUrlGroup(Id, uriPrefix, 0);
@ -117,7 +117,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS) if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS)
{ {
LogHelper.LogError(_logger, "CleanupV2Config", "Result: " + statusCode); _logger.LogError("CleanupV2Config; Result: " + statusCode);
} }
Id = 0; Id = 0;
} }

View File

@ -400,13 +400,13 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
else if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_INVALID_PARAMETER) else if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_INVALID_PARAMETER)
{ {
LogHelper.LogError(logger, "GetChannelBindingFromTls", "Channel binding is not supported."); logger.LogError("GetChannelBindingFromTls; Channel binding is not supported.");
return null; // old schannel library which doesn't support CBT return null; // old schannel library which doesn't support CBT
} }
else else
{ {
// It's up to the consumer to fail if the missing ChannelBinding matters to them. // It's up to the consumer to fail if the missing ChannelBinding matters to them.
LogHelper.LogException(logger, "GetChannelBindingFromTls", new HttpSysException((int)statusCode)); logger.LogError(0, new HttpSysException((int)statusCode), "GetChannelBindingFromTls");
break; break;
} }
} }

View File

@ -120,14 +120,14 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
if (!Request.IsHttps) if (!Request.IsHttps)
{ {
LogHelper.LogDebug(Logger, "TryGetChannelBinding", "Channel binding requires HTTPS."); Logger.LogDebug("TryGetChannelBinding; Channel binding requires HTTPS.");
return false; return false;
} }
value = ClientCertLoader.GetChannelBindingFromTls(Server.RequestQueue, Request.UConnectionId, Logger); value = ClientCertLoader.GetChannelBindingFromTls(Server.RequestQueue, Request.UConnectionId, Logger);
Debug.Assert(value != null, "GetChannelBindingFromTls returned null even though OS supposedly supports Extended Protection"); Debug.Assert(value != null, "GetChannelBindingFromTls returned null even though OS supposedly supports Extended Protection");
LogHelper.LogInfo(Logger, "Channel binding retrieved."); Logger.LogInformation("Channel binding retrieved.");
return value != null; return value != null;
} }
@ -177,7 +177,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Exception ex) catch (Exception ex)
{ {
LogHelper.LogDebug(Logger, "Abort", ex); Logger.LogDebug(0, ex, "Abort");
} }
_requestAbortSource.Dispose(); _requestAbortSource.Dispose();
} }
@ -231,7 +231,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
try try
{ {
var streamError = new HttpApiTypes.HTTP_REQUEST_PROPERTY_STREAM_ERROR() { ErrorCode = (uint)errorCode }; var streamError = new HttpApiTypes.HTTP_REQUEST_PROPERTY_STREAM_ERROR() { ErrorCode = (uint)errorCode };
var statusCode = HttpApi.HttpSetRequestProperty(Server.RequestQueue.Handle, Request.RequestId, HttpApiTypes.HTTP_REQUEST_PROPERTY.HttpRequestPropertyStreamError, (void*)&streamError, var statusCode = HttpApi.HttpSetRequestProperty(Server.RequestQueue.Handle, Request.RequestId, HttpApiTypes.HTTP_REQUEST_PROPERTY.HttpRequestPropertyStreamError, (void*)&streamError,
(uint)sizeof(HttpApiTypes.HTTP_REQUEST_PROPERTY_STREAM_ERROR), IntPtr.Zero); (uint)sizeof(HttpApiTypes.HTTP_REQUEST_PROPERTY_STREAM_ERROR), IntPtr.Zero);
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)

View File

@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS && statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_HANDLE_EOF) if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS && statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_HANDLE_EOF)
{ {
Exception exception = new IOException(string.Empty, new HttpSysException((int)statusCode)); Exception exception = new IOException(string.Empty, new HttpSysException((int)statusCode));
LogHelper.LogException(Logger, "Read", exception); Logger.LogError(0, exception, "Read");
Abort(); Abort();
throw exception; throw exception;
} }
@ -242,7 +242,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Exception e) catch (Exception e)
{ {
LogHelper.LogException(Logger, "BeginRead", e); Logger.LogError(0, e, "BeginRead");
asyncResult.Dispose(); asyncResult.Dispose();
throw; throw;
} }
@ -258,7 +258,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
else else
{ {
Exception exception = new IOException(string.Empty, new HttpSysException((int)statusCode)); Exception exception = new IOException(string.Empty, new HttpSysException((int)statusCode));
LogHelper.LogException(Logger, "BeginRead", exception); Logger.LogError(0, exception, "BeginRead");
Abort(); Abort();
throw exception; throw exception;
} }
@ -365,7 +365,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
asyncResult.Dispose(); asyncResult.Dispose();
Abort(); Abort();
LogHelper.LogException(Logger, "ReadAsync", e); Logger.LogError(0, e, "ReadAsync");
throw; throw;
} }
@ -386,7 +386,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
else else
{ {
Exception exception = new IOException(string.Empty, new HttpSysException((int)statusCode)); Exception exception = new IOException(string.Empty, new HttpSysException((int)statusCode));
LogHelper.LogException(Logger, "ReadAsync", exception); Logger.LogError(0, exception, "ReadAsync");
Abort(); Abort();
throw exception; throw exception;
} }

View File

@ -130,7 +130,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
if (!RequestContext.DisconnectToken.IsCancellationRequested) if (!RequestContext.DisconnectToken.IsCancellationRequested)
{ {
// This is logged rather than thrown because it is too late for an exception to be visible in user code. // This is logged rather than thrown because it is too late for an exception to be visible in user code.
LogHelper.LogError(Logger, "ResponseStream::Dispose", "Fewer bytes were written than were specified in the Content-Length."); Logger.LogError("ResponseStream::Dispose; Fewer bytes were written than were specified in the Content-Length.");
} }
_requestContext.Abort(); _requestContext.Abort();
return; return;
@ -175,14 +175,14 @@ namespace Microsoft.AspNetCore.Server.HttpSys
if (ThrowWriteExceptions) if (ThrowWriteExceptions)
{ {
var exception = new IOException(string.Empty, new HttpSysException((int)statusCode)); var exception = new IOException(string.Empty, new HttpSysException((int)statusCode));
LogHelper.LogException(Logger, "Flush", exception); Logger.LogError(0, exception, "Flush");
Abort(); Abort();
throw exception; throw exception;
} }
else else
{ {
// Abort the request but do not close the stream, let future writes complete silently // Abort the request but do not close the stream, let future writes complete silently
LogHelper.LogDebug(Logger, "Flush", $"Ignored write exception: {statusCode}"); Logger.LogDebug($"Flush; Ignored write exception: {statusCode}");
Abort(dispose: false); Abort(dispose: false);
} }
} }
@ -345,7 +345,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Exception e) catch (Exception e)
{ {
LogHelper.LogException(Logger, "FlushAsync", e); Logger.LogError(0, e, "FlushAsync");
asyncResult.Dispose(); asyncResult.Dispose();
Abort(); Abort();
throw; throw;
@ -355,21 +355,21 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
LogHelper.LogDebug(Logger, "FlushAsync", $"Write cancelled with error code: {statusCode}"); Logger.LogDebug($"FlushAsync; Write cancelled with error code: {statusCode}");
asyncResult.Cancel(ThrowWriteExceptions); asyncResult.Cancel(ThrowWriteExceptions);
} }
else if (ThrowWriteExceptions) else if (ThrowWriteExceptions)
{ {
asyncResult.Dispose(); asyncResult.Dispose();
Exception exception = new IOException(string.Empty, new HttpSysException((int)statusCode)); Exception exception = new IOException(string.Empty, new HttpSysException((int)statusCode));
LogHelper.LogException(Logger, "FlushAsync", exception); Logger.LogError(0, exception, "FlushAsync");
Abort(); Abort();
throw exception; throw exception;
} }
else else
{ {
// Abort the request but do not close the stream, let future writes complete silently // Abort the request but do not close the stream, let future writes complete silently
LogHelper.LogDebug(Logger, "FlushAsync", $"Ignored write exception: {statusCode}"); Logger.LogDebug($"FlushAsync; Ignored write exception: {statusCode}");
asyncResult.FailSilently(); asyncResult.FailSilently();
} }
} }
@ -639,7 +639,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Exception e) catch (Exception e)
{ {
LogHelper.LogException(Logger, "SendFileAsync", e); Logger.LogError(0, e, "SendFileAsync");
asyncResult.Dispose(); asyncResult.Dispose();
Abort(); Abort();
throw; throw;
@ -649,21 +649,21 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
LogHelper.LogDebug(Logger, "SendFileAsync", $"Write cancelled with error code: {statusCode}"); Logger.LogDebug($"SendFileAsync; Write cancelled with error code: {statusCode}");
asyncResult.Cancel(ThrowWriteExceptions); asyncResult.Cancel(ThrowWriteExceptions);
} }
else if (ThrowWriteExceptions) else if (ThrowWriteExceptions)
{ {
asyncResult.Dispose(); asyncResult.Dispose();
var exception = new IOException(string.Empty, new HttpSysException((int)statusCode)); var exception = new IOException(string.Empty, new HttpSysException((int)statusCode));
LogHelper.LogException(Logger, "SendFileAsync", exception); Logger.LogError(0, exception, "SendFileAsync");
Abort(); Abort();
throw exception; throw exception;
} }
else else
{ {
// Abort the request but do not close the stream, let future writes complete silently // Abort the request but do not close the stream, let future writes complete silently
LogHelper.LogDebug(Logger, "SendFileAsync", $"Ignored write exception: {statusCode}"); Logger.LogDebug($"SendFileAsync; Ignored write exception: {statusCode}");
asyncResult.FailSilently(); asyncResult.FailSilently();
} }
} }

View File

@ -8,6 +8,7 @@ using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.HttpSys.Internal; using Microsoft.AspNetCore.HttpSys.Internal;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Server.HttpSys namespace Microsoft.AspNetCore.Server.HttpSys
{ {
@ -232,18 +233,18 @@ namespace Microsoft.AspNetCore.Server.HttpSys
{ {
if (asyncResult._cancellationToken.IsCancellationRequested) if (asyncResult._cancellationToken.IsCancellationRequested)
{ {
LogHelper.LogDebug(logger, "FlushAsync.IOCompleted", $"Write cancelled with error code: {errorCode}"); logger.LogDebug($"FlushAsync.IOCompleted; Write cancelled with error code: {errorCode}");
asyncResult.Cancel(asyncResult._responseStream.ThrowWriteExceptions); asyncResult.Cancel(asyncResult._responseStream.ThrowWriteExceptions);
} }
else if (asyncResult._responseStream.ThrowWriteExceptions) else if (asyncResult._responseStream.ThrowWriteExceptions)
{ {
var exception = new IOException(string.Empty, new HttpSysException((int)errorCode)); var exception = new IOException(string.Empty, new HttpSysException((int)errorCode));
LogHelper.LogException(logger, "FlushAsync.IOCompleted", exception); logger.LogError(0, exception, "FlushAsync.IOCompleted");
asyncResult.Fail(exception); asyncResult.Fail(exception);
} }
else else
{ {
LogHelper.LogDebug(logger, "FlushAsync.IOCompleted", $"Ignored write exception: {errorCode}"); logger.LogDebug($"FlushAsync.IOCompleted; Ignored write exception: {errorCode}");
asyncResult.FailSilently(); asyncResult.FailSilently();
} }
} }
@ -258,7 +259,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
// TODO: Verbose log // TODO: Verbose log
// for (int i = 0; i < asyncResult._dataChunks.Length; i++) // for (int i = 0; i < asyncResult._dataChunks.Length; i++)
// { // {
// Logging.Dump(Logging.HttpListener, asyncResult, "Callback", (IntPtr)asyncResult._dataChunks[0].fromMemory.pBuffer, (int)asyncResult._dataChunks[0].fromMemory.BufferLength); // Logging.Dump(Logging.HttpListener, asyncResult, "Callback", (IntPtr)asyncResult._dataChunks[0].fromMemory.pBuffer, (int)asyncResult._dataChunks[0].fromMemory.BufferLength);
// } // }
} }
asyncResult.Complete(); asyncResult.Complete();
@ -266,7 +267,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
catch (Exception e) catch (Exception e)
{ {
LogHelper.LogException(logger, "FlushAsync.IOCompleted", e); logger.LogError(0, e, "FlushAsync.IOCompleted");
asyncResult.Fail(e); asyncResult.Fail(e);
} }
} }