Skip to content

Commit d477514

Browse files
author
Sebastien Roche
committed
FIX: modified GetLevel to accept both HttpContext and Exception. This is useful when an exception is thrown but HttpStatusCode is not yet set to 500 InternalServerError
FIX: DefaultGetLevel now returns Error if there is an exception OR if StatusCode > 499
1 parent 740fc2f commit d477514

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

Lines changed: 3 additions & 3 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, LogEventLevel> _getLevel;
32+
readonly Func<HttpContext, 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);
75+
var level = _getLevel(httpContext, ex);
7676

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

@@ -98,7 +98,7 @@ static double GetElapsedMilliseconds(long start, long stop)
9898
{
9999
return (stop - start) * 1000 / (double)Stopwatch.Frequency;
100100
}
101-
101+
102102
static string GetPath(HttpContext httpContext)
103103
{
104104
return httpContext.Features.Get<IHttpRequestFeature>()?.RawTarget ?? httpContext.Request.Path.ToString();

src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ 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 current <see cref="HttpContext"/>.
39-
/// The default behavior returns LogEventLevel.Error when HttpStatusCode is greater than 499
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.
4040
/// </summary>
4141
/// <value>
4242
/// The function returning the <see cref="LogEventLevel"/>.
4343
/// </value>
44-
public Func<HttpContext, LogEventLevel> GetLevel { get; set; }
44+
public Func<HttpContext, Exception, LogEventLevel> GetLevel { get; set; }
4545

4646
internal RequestLoggingOptions() { }
4747
}

src/Serilog.AspNetCore/SerilogApplicationBuilderExtensions.cs

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

31-
static Func<HttpContext, LogEventLevel> DefaultGetLevel =
32-
ctx => ctx.Response.StatusCode > 499 ? LogEventLevel.Error : LogEventLevel.Information;
31+
static Func<HttpContext, Exception, LogEventLevel> DefaultGetLevel =
32+
(ctx, ex) => ex != null
33+
? LogEventLevel.Error
34+
: ctx.Response.StatusCode > 499
35+
? LogEventLevel.Error
36+
: LogEventLevel.Information;
3337

3438
/// <summary>
3539
/// Adds middleware for streamlined request logging. Instead of writing HTTP request information

0 commit comments

Comments
 (0)