Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the aspnet core logging infrastructure for logging #304

Merged
merged 2 commits into from
Sep 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using System;
Expand Down Expand Up @@ -41,6 +42,7 @@ public abstract class APIGatewayProxyFunction
private AspNetCoreStartupMode _startupMode;
private IWebHost _host;
private APIGatewayServer _server;
private ILogger _logger;

// Defines a mapping from registered content types to the response encoding format
// which dictates what transformations should be applied before returning response content
Expand Down Expand Up @@ -143,6 +145,7 @@ protected void Start()
{
throw new Exception("Failed to find the implementation APIGatewayServer for the IServer registration. This can happen if UseApiGateway was not called.");
}
_logger = ActivatorUtilities.CreateInstance<Logger<APIGatewayProxyFunction>>(this._host.Services);
}

/// <summary>
Expand Down Expand Up @@ -220,16 +223,15 @@ protected virtual IWebHostBuilder CreateWebHostBuilder()
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public virtual async Task<APIGatewayProxyResponse> FunctionHandlerAsync(APIGatewayProxyRequest request, ILambdaContext lambdaContext)
{
lambdaContext.Logger.LogLine($"Incoming {request.HttpMethod} requests to {request.Path}");

if (!IsStarted)
{
Start();
}
_logger.LogDebug($"Incoming {request.HttpMethod} requests to {request.Path}");

InvokeFeatures features = new InvokeFeatures();
MarshallRequest(features, request, lambdaContext);
lambdaContext.Logger.LogLine($"ASP.NET Core Request PathBase: {((IHttpRequestFeature)features).PathBase}, Path: {((IHttpRequestFeature)features).Path}");
_logger.LogDebug($"ASP.NET Core Request PathBase: {((IHttpRequestFeature)features).PathBase}, Path: {((IHttpRequestFeature)features).Path}");

var context = this.CreateContext(features);

Expand All @@ -238,7 +240,7 @@ public virtual async Task<APIGatewayProxyResponse> FunctionHandlerAsync(APIGatew
var identity = new ClaimsIdentity(request.RequestContext.Authorizer.Claims.Select(
entry => new Claim(entry.Key, entry.Value.ToString())), "AuthorizerIdentity");

lambdaContext.Logger.LogLine($"Configuring HttpContext.User with {request.RequestContext.Authorizer.Claims.Count} claims coming from API Gateway's Request Context");
_logger.LogDebug($"Configuring HttpContext.User with {request.RequestContext.Authorizer.Claims.Count} claims coming from API Gateway's Request Context");
context.HttpContext.User = new ClaimsPrincipal(identity);
}

Expand Down Expand Up @@ -312,20 +314,20 @@ protected async Task<APIGatewayProxyResponse> ProcessRequest(ILambdaContext lamb
catch (AggregateException agex)
{
ex = agex;
lambdaContext.Logger.LogLine($"Caught AggregateException: '{agex}'");
_logger.LogError($"Caught AggregateException: '{agex}'");
var sb = new StringBuilder();
foreach (var newEx in agex.InnerExceptions)
{
sb.AppendLine(this.ErrorReport(newEx));
}

lambdaContext.Logger.LogLine(sb.ToString());
_logger.LogError(sb.ToString());
defaultStatusCode = 500;
}
catch (ReflectionTypeLoadException rex)
{
ex = rex;
lambdaContext.Logger.LogLine($"Caught ReflectionTypeLoadException: '{rex}'");
_logger.LogError($"Caught ReflectionTypeLoadException: '{rex}'");
var sb = new StringBuilder();
foreach (var loaderException in rex.LoaderExceptions)
{
Expand All @@ -340,14 +342,14 @@ protected async Task<APIGatewayProxyResponse> ProcessRequest(ILambdaContext lamb
}
}

lambdaContext.Logger.LogLine(sb.ToString());
_logger.LogError(sb.ToString());
defaultStatusCode = 500;
}
catch (Exception e)
{
ex = e;
if (rethrowUnhandledError) throw;
lambdaContext.Logger.LogLine($"Unknown error responding to request: {this.ErrorReport(e)}");
_logger.LogError($"Unknown error responding to request: {this.ErrorReport(e)}");
defaultStatusCode = 500;
}
finally
Expand All @@ -361,7 +363,7 @@ protected async Task<APIGatewayProxyResponse> ProcessRequest(ILambdaContext lamb
}
var response = this.MarshallResponse(features, lambdaContext, defaultStatusCode);

lambdaContext.Logger.LogLine($"Response Base 64 Encoded: {response.IsBase64Encoded}");
_logger.LogDebug($"Response Base 64 Encoded: {response.IsBase64Encoded}");

if (ex != null)
response.Headers.Add(new KeyValuePair<string, string>("ErrorType", ex.GetType().Name));
Expand Down