Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 9c9cd87

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
strong type Log handler options
1 parent 181eb0a commit 9c9cd87

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

Community.AspNetCore.ExceptionHandling/Logs/LogExceptionHandler.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Commmunity.AspNetCore.ExceptionHandling.Logs
1010
{
11-
public class LogExceptionHandler<TException> : IExceptionHandler
11+
public class LogExceptionHandler<TException> : HandlerStrongType<TException>
1212
where TException : Exception
1313
{
1414
private readonly IOptions<LogHandlerOptions<TException>> _settings;
@@ -17,11 +17,12 @@ public class LogExceptionHandler<TException> : IExceptionHandler
1717

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

20-
public LogExceptionHandler(IOptions<LogHandlerOptions<TException>> settings)
20+
public LogExceptionHandler(IOptions<LogHandlerOptions<TException>> settings, ILoggerFactory loggerFactory):base(settings.Value, loggerFactory)
2121
{
2222
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
2323
}
24-
public Task<HandlerResult> Handle(HttpContext httpContext, Exception exception)
24+
25+
protected override Task<HandlerResult> HandleStrongType(HttpContext httpContext, TException exception)
2526
{
2627
var logLevel = this.Settings.Level?.Invoke(httpContext, exception) ?? LogLevel.Error;
2728

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

4445
EventId eventId = this.Settings.EventIdFactory != null
4546
? this.Settings.EventIdFactory(httpContext, exception)
46-
: DefaultEvent;
47+
: DefaultEvent;
4748

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

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

54-
logger.Log(logLevel, eventId, state, exception, formatter);
55+
if (this.Settings.Formatter != null)
56+
{
57+
formatter = (o, e) => this.Settings.Formatter(0, e as TException);
58+
}
59+
else
60+
{
61+
formatter = (o, e) => o.ToString();
62+
}
63+
64+
logger.Log(logLevel, eventId, state, exception, formatter);
5565
}
56-
66+
5767
return Task.FromResult(HandlerResult.NextHandler);
5868
}
5969
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Commmunity.AspNetCore.ExceptionHandling.Handlers;
23
using Microsoft.AspNetCore.Http;
34
using Microsoft.Extensions.Logging;
45
using Microsoft.Extensions.Logging.Internal;
@@ -12,34 +13,34 @@ namespace Commmunity.AspNetCore.ExceptionHandling.Logs
1213
/// <typeparam name="TException">
1314
/// The exception type
1415
/// </typeparam>
15-
public class LogHandlerOptions<TException> : IOptions<LogHandlerOptions<TException>>
16+
public class LogHandlerOptions<TException> : HandlerWithLoggerOptions, IOptions<LogHandlerOptions<TException>>
1617
where TException : Exception
1718
{
1819
public LogHandlerOptions<TException> Value => this;
1920

2021
/// <summary>
2122
/// Factory for <see cref="EventId"/>
2223
/// </summary>
23-
public Func<HttpContext, Exception, EventId> EventIdFactory { get; set; }
24+
public Func<HttpContext, TException, EventId> EventIdFactory { get; set; }
2425

2526
/// <summary>
2627
/// The Formatter. By default state.ToString().
2728
/// </summary>
28-
public Func<object, Exception, string> Formatter { get; set; }
29+
public Func<object, TException, string> Formatter { get; set; }
2930

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

3536
/// <summary>
3637
/// Factory for log category. By default "Commmunity.AspNetCore.ExceptionHandling" will be used.
3738
/// </summary>
38-
public Func<HttpContext, Exception, string> Category { get; set; }
39+
public Func<HttpContext, TException, string> Category { get; set; }
3940

4041
/// <summary>
4142
/// 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.
4243
/// </summary>
43-
public Func<HttpContext, Exception, LogLevel> Level { get; set; }
44+
public Func<HttpContext, TException, LogLevel> Level { get; set; }
4445
}
4546
}

0 commit comments

Comments
 (0)