Skip to content

Commit

Permalink
strong type Log handler options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihar Yakimush authored and Ihar Yakimush committed Jul 17, 2018
1 parent 181eb0a commit 9c9cd87
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
24 changes: 17 additions & 7 deletions Community.AspNetCore.ExceptionHandling/Logs/LogExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Commmunity.AspNetCore.ExceptionHandling.Logs
{
public class LogExceptionHandler<TException> : IExceptionHandler
public class LogExceptionHandler<TException> : HandlerStrongType<TException>
where TException : Exception
{
private readonly IOptions<LogHandlerOptions<TException>> _settings;
Expand All @@ -17,11 +17,12 @@ public class LogExceptionHandler<TException> : IExceptionHandler

public LogHandlerOptions<TException> Settings => this._settings.Value;

public LogExceptionHandler(IOptions<LogHandlerOptions<TException>> settings)
public LogExceptionHandler(IOptions<LogHandlerOptions<TException>> settings, ILoggerFactory loggerFactory):base(settings.Value, loggerFactory)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
}
public Task<HandlerResult> Handle(HttpContext httpContext, Exception exception)

protected override Task<HandlerResult> HandleStrongType(HttpContext httpContext, TException exception)
{
var logLevel = this.Settings.Level?.Invoke(httpContext, exception) ?? LogLevel.Error;

Expand All @@ -43,17 +44,26 @@ public Task<HandlerResult> Handle(HttpContext httpContext, Exception exception)

EventId eventId = this.Settings.EventIdFactory != null
? this.Settings.EventIdFactory(httpContext, exception)
: DefaultEvent;
: DefaultEvent;

object state = this.Settings.StateFactory?.Invoke(httpContext, exception, this.Settings) ??
new FormattedLogValues("Unhandled error occured. RequestId: {requestId}.",
httpContext.TraceIdentifier);

Func<object, Exception, string> formatter = this.Settings.Formatter ?? ((o, e) => o.ToString());
Func<object, Exception, string> formatter;

logger.Log(logLevel, eventId, state, exception, formatter);
if (this.Settings.Formatter != null)
{
formatter = (o, e) => this.Settings.Formatter(0, e as TException);
}
else
{
formatter = (o, e) => o.ToString();
}

logger.Log(logLevel, eventId, state, exception, formatter);
}

return Task.FromResult(HandlerResult.NextHandler);
}
}
Expand Down
13 changes: 7 additions & 6 deletions Community.AspNetCore.ExceptionHandling/Logs/LogHandlerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Commmunity.AspNetCore.ExceptionHandling.Handlers;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Internal;
Expand All @@ -12,34 +13,34 @@ namespace Commmunity.AspNetCore.ExceptionHandling.Logs
/// <typeparam name="TException">
/// The exception type
/// </typeparam>
public class LogHandlerOptions<TException> : IOptions<LogHandlerOptions<TException>>
public class LogHandlerOptions<TException> : HandlerWithLoggerOptions, IOptions<LogHandlerOptions<TException>>
where TException : Exception
{
public LogHandlerOptions<TException> Value => this;

/// <summary>
/// Factory for <see cref="EventId"/>
/// </summary>
public Func<HttpContext, Exception, EventId> EventIdFactory { get; set; }
public Func<HttpContext, TException, EventId> EventIdFactory { get; set; }

/// <summary>
/// The Formatter. By default state.ToString().
/// </summary>
public Func<object, Exception, string> Formatter { get; set; }
public Func<object, TException, string> Formatter { get; set; }

/// <summary>
/// Foctory for log entry state. By default <see cref="FormattedLogValues"/> with TraceIdentifier.
/// </summary>
public Func<HttpContext, Exception, LogHandlerOptions<TException>, object> StateFactory { get; set; }
public Func<HttpContext, TException, LogHandlerOptions<TException>, object> StateFactory { get; set; }

/// <summary>
/// Factory for log category. By default "Commmunity.AspNetCore.ExceptionHandling" will be used.
/// </summary>
public Func<HttpContext, Exception, string> Category { get; set; }
public Func<HttpContext, TException, string> Category { get; set; }

/// <summary>
/// Factory for <see cref="LogLevel"/> log level. By default <see cref="LogLevel.Error"/> error will be used. In case of <see cref="LogLevel.None"/> none log entry will be skipped.
/// </summary>
public Func<HttpContext, Exception, LogLevel> Level { get; set; }
public Func<HttpContext, TException, LogLevel> Level { get; set; }
}
}

0 comments on commit 9c9cd87

Please sign in to comment.