Skip to content

Allow an ILogger to be specified #183

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 1 commit into from
May 19, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

namespace Serilog.AspNetCore
{
// ReSharper disable once ClassNeverInstantiated.Global
class RequestLoggingMiddleware
{
readonly RequestDelegate _next;
readonly DiagnosticContext _diagnosticContext;
readonly MessageTemplate _messageTemplate;
readonly Action<IDiagnosticContext, HttpContext> _enrichDiagnosticContext;
readonly Func<HttpContext, double, Exception, LogEventLevel> _getLevel;
readonly ILogger _logger;
static readonly LogEventProperty[] NoProperties = new LogEventProperty[0];

public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options)
Expand All @@ -42,6 +44,7 @@ public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnost
_getLevel = options.GetLevel;
_enrichDiagnosticContext = options.EnrichDiagnosticContext;
_messageTemplate = new MessageTemplateParser().Parse(options.MessageTemplate);
_logger = options.Logger?.ForContext<RequestLoggingMiddleware>();
}

// ReSharper disable once UnusedMember.Global
Expand Down Expand Up @@ -73,7 +76,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 logger = _logger ?? Log.ForContext<RequestLoggingMiddleware>();
var level = _getLevel(httpContext, elapsedMs, ex);

if (!logger.IsEnabled(level)) return false;
Expand Down
9 changes: 9 additions & 0 deletions src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using Serilog.Events;
using System;

// ReSharper disable UnusedAutoPropertyAccessor.Global

namespace Serilog.AspNetCore
{
/// <summary>
Expand Down Expand Up @@ -50,6 +52,13 @@ public class RequestLoggingOptions
/// </summary>
public Action<IDiagnosticContext, HttpContext> EnrichDiagnosticContext { get; set; }


/// <summary>
/// The logger through which request completion events will be logged. The default is to use the
/// static <see cref="Log"/> class.
/// </summary>
public ILogger Logger { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/serilog/serilog-aspnetcore/blob/dev/src/Serilog.AspNetCore/SerilogWebHostBuilderExtensions.cs#L146

I think it is worth to use DI provided features. Why to manually specify logger if it is already exists ? Just add new ctor param:

 public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options, ILogger logger)

Copy link

@dazinator dazinator Mar 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sungam3r - what about if serilog ILogger is not registered for DI and the developer is only using Log.Logger everywhere - which i may be wrong, but is a valid style for using serilog?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, then we can use second RequestLoggingMiddleware ctor with ILogger property inside options to satisfy both categories.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @sungam3r - will give it some thought 👍


internal RequestLoggingOptions() { }
}
}