Skip to content

Commit 2d741c1

Browse files
authored
Merge pull request #137 from serilog/get-level-elapsed
Include 'double elapsedMs' in the function signature of the GetLevel delegate
2 parents 731e583 + ac78f96 commit 2d741c1

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class RequestLoggingMiddleware
2929
readonly RequestDelegate _next;
3030
readonly DiagnosticContext _diagnosticContext;
3131
readonly MessageTemplate _messageTemplate;
32-
readonly Func<HttpContext, Exception, LogEventLevel> _getLevel;
32+
readonly Func<HttpContext, double, Exception, LogEventLevel> _getLevel;
3333
static readonly LogEventProperty[] NoProperties = new LogEventProperty[0];
3434

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

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

src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ public class RequestLoggingOptions
3535
public string MessageTemplate { get; set; }
3636

3737
/// <summary>
38-
/// 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
39-
/// The default behavior returns LogEventLevel.Error when HttpStatusCode is greater than 499 or if Exception is not null.
38+
/// Gets or sets the function returning the <see cref="LogEventLevel"/> based on the <see cref="HttpContext"/>, the number of
39+
/// elapsed milliseconds required for handling the request, and an <see cref="Exception" /> if one was thrown.
40+
/// The default behavior returns <see cref="LogEventLevel.Error"/> when the response status code is greater than 499 or if the
41+
/// <see cref="Exception"/> is not null.
4042
/// </summary>
4143
/// <value>
4244
/// The function returning the <see cref="LogEventLevel"/>.
4345
/// </value>
44-
public Func<HttpContext, Exception, LogEventLevel> GetLevel { get; set; }
46+
public Func<HttpContext, double, Exception, LogEventLevel> GetLevel { get; set; }
4547

4648
internal RequestLoggingOptions() { }
4749
}

src/Serilog.AspNetCore/SerilogApplicationBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public static class SerilogApplicationBuilderExtensions
2828
const string DefaultRequestCompletionMessageTemplate =
2929
"HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
3030

31-
static Func<HttpContext, Exception, LogEventLevel> DefaultGetLevel =
32-
(ctx, ex) => ex != null
31+
static LogEventLevel DefaultGetLevel(HttpContext ctx, double _, Exception ex) =>
32+
ex != null
3333
? LogEventLevel.Error
3434
: ctx.Response.StatusCode > 499
3535
? LogEventLevel.Error

0 commit comments

Comments
 (0)