Skip to content

Commit f35e12e

Browse files
author
Sebastien Roche
committed
FEATURE: update sdk version to target latest stable .net core SDK
FEATURE: use default empty overload in sample FEATURE: rename GetLogEventLevel to GetLogLevel
1 parent 5341d20 commit f35e12e

File tree

5 files changed

+28
-36
lines changed

5 files changed

+28
-36
lines changed

global.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "2.2.105"
3+
"version": "2.2.402"
44
}
5-
}
5+
}

samples/InlineInitializationSample/Startup.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
4848
// Write streamlined request completion events, instead of the more verbose ones from the framework.
4949
// To use the default framework request logging instead, remove this line and set the "Microsoft"
5050
// level in appsettings.json to "Information".
51-
app.UseSerilogRequestLogging(opts =>
52-
{
53-
opts.GetLogEventLevel = statusCode =>
54-
statusCode == HttpStatusCode.InternalServerError
55-
? LogEventLevel.Error
56-
: LogEventLevel.Information;
57-
// OR opts.GetLogEventLevel = _ => LogEventLevel.Information;
58-
});
51+
app.UseSerilogRequestLogging();
5952

6053
app.UseStaticFiles();
6154
app.UseCookiePolicy();

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,33 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using System;
16-
using System.Diagnostics;
17-
using System.Linq;
18-
using System.Net;
19-
using System.Threading.Tasks;
2015
using Microsoft.AspNetCore.Http;
2116
using Microsoft.AspNetCore.Http.Features;
2217
using Serilog.Events;
2318
using Serilog.Extensions.Hosting;
2419
using Serilog.Parsing;
20+
using System;
21+
using System.Diagnostics;
22+
using System.Linq;
23+
using System.Threading.Tasks;
2524

2625
namespace Serilog.AspNetCore
2726
{
28-
class RequestLoggingMiddleware
27+
internal class RequestLoggingMiddleware
2928
{
3029
readonly RequestDelegate _next;
3130
readonly DiagnosticContext _diagnosticContext;
3231
readonly MessageTemplate _messageTemplate;
33-
readonly Func<HttpStatusCode, LogEventLevel> _getLogEventLevel;
34-
32+
readonly Func<HttpContext, LogEventLevel> _getLogLevel;
3533
static readonly LogEventProperty[] NoProperties = new LogEventProperty[0];
36-
34+
3735
public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options)
3836
{
3937
if (options == null) throw new ArgumentNullException(nameof(options));
4038
_next = next ?? throw new ArgumentNullException(nameof(next));
4139
_diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
4240

43-
_getLogEventLevel = options.GetLogEventLevel;
41+
_getLogLevel = options.GetLogLevel;
4442
_messageTemplate = new MessageTemplateParser().Parse(options.MessageTemplate);
4543
}
4644

@@ -71,13 +69,13 @@ public async Task Invoke(HttpContext httpContext)
7169
}
7270
}
7371

74-
bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector, int statusCode, double elapsedMs, Exception ex)
72+
private bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector, int statusCode, double elapsedMs, Exception ex)
7573
{
7674
var logger = Log.ForContext<RequestLoggingMiddleware>();
77-
var level = _getLogEventLevel((HttpStatusCode)statusCode);
78-
75+
var level = _getLogLevel(httpContext);
76+
7977
if (!logger.IsEnabled(level)) return false;
80-
78+
8179
if (!collector.TryComplete(out var collectedProperties))
8280
collectedProperties = NoProperties;
8381

src/Serilog.AspNetCore/RequestLoggingOptions.cs renamed to src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using Microsoft.AspNetCore.Http;
1516
using Serilog.Events;
1617
using System;
17-
using System.Net;
1818

19-
namespace Serilog
19+
namespace Serilog.AspNetCore
2020
{
2121
/// <summary>
22-
/// Contains options for the Serilog.AspNetCore.RequestLoggingMiddleware.
22+
/// Contains options for the <see cref="Serilog.AspNetCore.RequestLoggingMiddleware"/>.
2323
/// </summary>
2424
public class RequestLoggingOptions
2525
{
@@ -35,13 +35,13 @@ public class RequestLoggingOptions
3535
public string MessageTemplate { get; set; }
3636

3737
/// <summary>
38-
/// Gets or sets the function returning the LogEventLevel based on the HttpStatusCode.
38+
/// Gets or sets the function returning the <see cref="LogEventLevel"/> based on the current <see cref="HttpContext"/>.
3939
/// The default behavior returns LogEventLevel.Error when HttpStatusCode is greater than 499
4040
/// </summary>
4141
/// <value>
42-
/// The function returning the LogEventLevel.
42+
/// The function returning the <see cref="LogEventLevel"/>.
4343
/// </value>
44-
public Func<HttpStatusCode, LogEventLevel> GetLogEventLevel { get; set; }
44+
public Func<HttpContext, LogEventLevel> GetLogLevel { get; set; }
4545

4646
internal RequestLoggingOptions() { }
4747
}

src/Serilog.AspNetCore/SerilogApplicationBuilderExtensions.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Net;
1717
using Microsoft.AspNetCore.Builder;
18+
using Microsoft.AspNetCore.Http;
1819
using Microsoft.Extensions.Options;
1920
using Serilog.AspNetCore;
2021
using Serilog.Events;
@@ -29,8 +30,8 @@ public static class SerilogApplicationBuilderExtensions
2930
const string DefaultRequestCompletionMessageTemplate =
3031
"HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
3132

32-
static readonly Func<HttpStatusCode, LogEventLevel> DefaultGetLogEventLevel =
33-
s => (int) s > 499 ? LogEventLevel.Error : LogEventLevel.Information;
33+
static Func<HttpContext, LogEventLevel> DefaultGetLogLevel =
34+
ctx => ctx.Response.StatusCode > 499 ? LogEventLevel.Error : LogEventLevel.Information;
3435

3536
/// <summary>
3637
/// Adds middleware for streamlined request logging. Instead of writing HTTP request information
@@ -60,7 +61,7 @@ public static IApplicationBuilder UseSerilogRequestLogging(
6061
/// in <c>Startup.cs</c> before any handlers whose activities should be logged.
6162
/// </summary>
6263
/// <param name="app">The application builder.</param>
63-
/// <param name="configureOptions"> An System.Action`1 to configure the provided <see cref="Serilog.RequestLoggingOptions" />.</param>
64+
/// <param name="configureOptions">A <see cref="System.Action{T}" /> to configure the provided <see cref="RequestLoggingOptions" />.</param>
6465
/// <returns>The application builder.</returns>
6566
public static IApplicationBuilder UseSerilogRequestLogging(
6667
this IApplicationBuilder app,
@@ -70,15 +71,15 @@ public static IApplicationBuilder UseSerilogRequestLogging(
7071

7172
var opts = new RequestLoggingOptions
7273
{
73-
GetLogEventLevel = DefaultGetLogEventLevel,
74+
GetLogLevel = DefaultGetLogLevel,
7475
MessageTemplate = DefaultRequestCompletionMessageTemplate
7576
};
7677
configureOptions?.Invoke(opts);
7778

7879
if (opts.MessageTemplate == null)
7980
throw new ArgumentException($"{nameof(opts.MessageTemplate)} cannot be null.");
80-
if (opts.GetLogEventLevel == null)
81-
throw new ArgumentException($"{nameof(opts.GetLogEventLevel)} cannot be null.");
81+
if (opts.GetLogLevel == null)
82+
throw new ArgumentException($"{nameof(opts.GetLogLevel)} cannot be null.");
8283

8384
return app.UseMiddleware<RequestLoggingMiddleware>(opts);
8485
}

0 commit comments

Comments
 (0)