Skip to content

Include 'double elapsedMs' in the function signature of the GetLevel delegate #137

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

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RequestLoggingMiddleware
readonly RequestDelegate _next;
readonly DiagnosticContext _diagnosticContext;
readonly MessageTemplate _messageTemplate;
readonly Func<HttpContext, Exception, LogEventLevel> _getLevel;
readonly Func<HttpContext, double, Exception, LogEventLevel> _getLevel;
static readonly LogEventProperty[] NoProperties = new LogEventProperty[0];

public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options)
Expand Down Expand Up @@ -72,7 +72,7 @@ public async Task Invoke(HttpContext httpContext)
bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector, int statusCode, double elapsedMs, Exception ex)
{
var logger = Log.ForContext<RequestLoggingMiddleware>();
var level = _getLevel(httpContext, ex);
var level = _getLevel(httpContext, elapsedMs, ex);

if (!logger.IsEnabled(level)) return false;

Expand Down
8 changes: 5 additions & 3 deletions src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public class RequestLoggingOptions
public string MessageTemplate { get; set; }

/// <summary>
/// Gets or sets the function returning the <see cref="LogEventLevel"/> based on the <see cref="HttpContext"/> and on the <see cref="Exception" /> if something wrong happend
/// The default behavior returns LogEventLevel.Error when HttpStatusCode is greater than 499 or if Exception is not null.
/// Gets or sets the function returning the <see cref="LogEventLevel"/> based on the <see cref="HttpContext"/>, the number of
/// elapsed milliseconds required for handling the request, and an <see cref="Exception" /> if one was thrown.
/// The default behavior returns <see cref="LogEventLevel.Error"/> when the response status code is greater than 499 or if the
/// <see cref="Exception"/> is not null.
/// </summary>
/// <value>
/// The function returning the <see cref="LogEventLevel"/>.
/// </value>
public Func<HttpContext, Exception, LogEventLevel> GetLevel { get; set; }
public Func<HttpContext, double, Exception, LogEventLevel> GetLevel { get; set; }

internal RequestLoggingOptions() { }
}
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog.AspNetCore/SerilogApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static class SerilogApplicationBuilderExtensions
const string DefaultRequestCompletionMessageTemplate =
"HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";

static Func<HttpContext, Exception, LogEventLevel> DefaultGetLevel =
(ctx, ex) => ex != null
static LogEventLevel DefaultGetLevel(HttpContext ctx, double _, Exception ex) =>
ex != null
? LogEventLevel.Error
: ctx.Response.StatusCode > 499
? LogEventLevel.Error
Expand Down