1313// limitations under the License.
1414
1515using System ;
16+ using System . Net ;
1617using Microsoft . AspNetCore . Builder ;
18+ using Microsoft . Extensions . Options ;
1719using Serilog . AspNetCore ;
20+ using Serilog . Events ;
1821
1922namespace Serilog
2023{
@@ -26,6 +29,9 @@ public static class SerilogApplicationBuilderExtensions
2629 const string DefaultRequestCompletionMessageTemplate =
2730 "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms" ;
2831
32+ static readonly Func < HttpStatusCode , LogEventLevel > DefaultGetLogEventLevel =
33+ s => ( int ) s > 499 ? LogEventLevel . Error : LogEventLevel . Information ;
34+
2935 /// <summary>
3036 /// Adds middleware for streamlined request logging. Instead of writing HTTP request information
3137 /// like method, path, timing, status code and exception details
@@ -43,11 +49,38 @@ public static class SerilogApplicationBuilderExtensions
4349 /// <returns>The application builder.</returns>
4450 public static IApplicationBuilder UseSerilogRequestLogging (
4551 this IApplicationBuilder app ,
46- string messageTemplate = DefaultRequestCompletionMessageTemplate )
52+ string messageTemplate )
53+ => app . UseSerilogRequestLogging ( opts => opts . MessageTemplate = messageTemplate ) ;
54+
55+ /// <summary>
56+ /// Adds middleware for streamlined request logging. Instead of writing HTTP request information
57+ /// like method, path, timing, status code and exception details
58+ /// in several events, this middleware collects information during the request (including from
59+ /// <see cref="IDiagnosticContext"/>), and writes a single event at request completion. Add this
60+ /// in <c>Startup.cs</c> before any handlers whose activities should be logged.
61+ /// </summary>
62+ /// <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+ /// <returns>The application builder.</returns>
65+ public static IApplicationBuilder UseSerilogRequestLogging (
66+ this IApplicationBuilder app ,
67+ Action < RequestLoggingOptions > configureOptions = null )
4768 {
4869 if ( app == null ) throw new ArgumentNullException ( nameof ( app ) ) ;
49- if ( messageTemplate == null ) throw new ArgumentNullException ( nameof ( messageTemplate ) ) ;
50- return app . UseMiddleware < RequestLoggingMiddleware > ( new RequestLoggingOptions ( messageTemplate ) ) ;
70+
71+ var opts = new RequestLoggingOptions
72+ {
73+ GetLogEventLevel = DefaultGetLogEventLevel ,
74+ MessageTemplate = DefaultRequestCompletionMessageTemplate
75+ } ;
76+ configureOptions ? . Invoke ( opts ) ;
77+
78+ if ( opts . MessageTemplate == null )
79+ throw new ArgumentException ( $ "{ nameof ( opts . MessageTemplate ) } cannot be null.") ;
80+ if ( opts . GetLogEventLevel == null )
81+ throw new ArgumentException ( $ "{ nameof ( opts . GetLogEventLevel ) } cannot be null.") ;
82+
83+ return app . UseMiddleware < RequestLoggingMiddleware > ( opts ) ;
5184 }
5285 }
53- }
86+ }
0 commit comments