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

Commit c316aea

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
add API comments
1 parent 469caf8 commit c316aea

File tree

11 files changed

+352
-35
lines changed

11 files changed

+352
-35
lines changed

Commmunity.AspNetCore.ExceptionHandling.Integration/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public void ConfigureServices(IServiceCollection services)
4545

4646
options.For<Exception>()
4747
.Log(lo => { lo.Formatter = (o, e) => "qwe"; })
48-
//.Response(e => 500, RequestStartedBehaviour.SkipHandler).ClearCacheHeaders().WithBodyJson((r, e) => new { msg = e.Message, path = r.Path })
49-
.Response(e => 500, RequestStartedBehaviour.SkipHandler).ClearCacheHeaders().WithBodyObjectResult((r, e) => new { msg = e.Message, path = r.Path })
48+
//.Response(e => 500, ResponseAlreadyStartedBehaviour.GoToNextHandler).ClearCacheHeaders().WithBodyJson((r, e) => new { msg = e.Message, path = r.Path })
49+
.Response(e => 500, ResponseAlreadyStartedBehaviour.GoToNextHandler).ClearCacheHeaders().WithObjectResult((r, e) => new { msg = e.Message, path = r.Path })
5050
.Handled();
5151
});
5252

Commmunity.AspNetCore.ExceptionHandling.Mvc/PolicyBuilderExtensions.cs

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,28 @@ public static class PolicyBuilderExtensions
1515

1616
private static readonly ActionDescriptor EmptyActionDescriptor = new ActionDescriptor();
1717

18-
public static IResponseHandlers<TException> WithBodyActionResult<TException, TResult>(
18+
/// <summary>
19+
/// Set <see cref="IActionResult"/> to response and pass control to next handler.
20+
/// </summary>
21+
/// <typeparam name="TException">
22+
/// The exception type
23+
/// </typeparam>
24+
/// <typeparam name="TResult">
25+
/// The action result type. Should implement <see cref="IActionResult"/>.
26+
/// </typeparam>
27+
/// <param name="builder">
28+
/// The policy builder
29+
/// </param>
30+
/// <param name="resultFactory">
31+
/// The <see cref="IActionResult"/> factory.
32+
/// </param>
33+
/// <param name="index" optional="true">
34+
/// Handler index in the chain. Optional. By default handler added to the end of chain.
35+
/// </param>
36+
/// <returns>
37+
/// Policy builder
38+
/// </returns>
39+
public static IResponseHandlers<TException> WithActionResult<TException, TResult>(
1940
this IResponseHandlers<TException> builder, Func<HttpRequest, TException, TResult> resultFactory, int index = -1)
2041
where TException : Exception
2142
where TResult : IActionResult
@@ -38,26 +59,89 @@ public static IResponseHandlers<TException> WithBodyActionResult<TException, TRe
3859
});
3960
}
4061

41-
public static IResponseHandlers<TException> WithBodyActionResult<TException, TResult>(
62+
/// <summary>
63+
/// Set <see cref="IActionResult"/> to response and pass control to next handler.
64+
/// </summary>
65+
/// <typeparam name="TException">
66+
/// The exception type
67+
/// </typeparam>
68+
/// <typeparam name="TResult">
69+
/// The action result type. Should implement <see cref="IActionResult"/>.
70+
/// </typeparam>
71+
/// <param name="builder">
72+
/// The policy builder
73+
/// </param>
74+
/// <param name="result">
75+
/// The <see cref="IActionResult"/> action result.
76+
/// </param>
77+
/// <param name="index" optional="true">
78+
/// Handler index in the chain. Optional. By default handler added to the end of chain.
79+
/// </param>
80+
/// <returns>
81+
/// Policy builder
82+
/// </returns>
83+
public static IResponseHandlers<TException> WithActionResult<TException, TResult>(
4284
this IResponseHandlers<TException> builder, TResult result, int index = -1)
4385
where TException : Exception
4486
where TResult : IActionResult
4587
{
46-
return builder.WithBodyActionResult((request, exception) => result);
88+
return builder.WithActionResult((request, exception) => result);
4789
}
4890

49-
public static IResponseHandlers<TException> WithBodyObjectResult<TException, TObject>(
91+
/// <summary>
92+
/// Set <see cref="ObjectResult"/> to response and pass control to next handler.
93+
/// </summary>
94+
/// <typeparam name="TException">
95+
/// The exception type
96+
/// </typeparam>
97+
/// <typeparam name="TObject">
98+
/// The result object type.
99+
/// </typeparam>
100+
/// <param name="builder">
101+
/// The policy builder
102+
/// </param>
103+
/// <param name="value">
104+
/// The result object.
105+
/// </param>
106+
/// <param name="index" optional="true">
107+
/// Handler index in the chain. Optional. By default handler added to the end of chain.
108+
/// </param>
109+
/// <returns>
110+
/// Policy builder
111+
/// </returns>
112+
public static IResponseHandlers<TException> WithObjectResult<TException, TObject>(
50113
this IResponseHandlers<TException> builder, TObject value, int index = -1)
51114
where TException : Exception
52115
{
53-
return builder.WithBodyActionResult(new ObjectResult(value), index);
116+
return builder.WithActionResult(new ObjectResult(value), index);
54117
}
55118

56-
public static IResponseHandlers<TException> WithBodyObjectResult<TException, TObject>(
119+
/// <summary>
120+
/// Set <see cref="ObjectResult"/> to response and pass control to next handler.
121+
/// </summary>
122+
/// <typeparam name="TException">
123+
/// The exception type
124+
/// </typeparam>
125+
/// <typeparam name="TObject">
126+
/// The result object type.
127+
/// </typeparam>
128+
/// <param name="builder">
129+
/// The policy builder
130+
/// </param>
131+
/// <param name="valueFactory">
132+
/// The result object factory.
133+
/// </param>
134+
/// <param name="index" optional="true">
135+
/// Handler index in the chain. Optional. By default handler added to the end of chain.
136+
/// </param>
137+
/// <returns>
138+
/// Policy builder
139+
/// </returns>
140+
public static IResponseHandlers<TException> WithObjectResult<TException, TObject>(
57141
this IResponseHandlers<TException> builder, Func<HttpRequest, TException, TObject> valueFactory, int index = -1)
58142
where TException : Exception
59143
{
60-
return builder.WithBodyActionResult(
144+
return builder.WithActionResult(
61145
(request, exception) => new ObjectResult(valueFactory(request, exception)), index);
62146
}
63147
}

Commmunity.AspNetCore.ExceptionHandling.NewtonsoftJson/PolicyBuilderExtensions.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,33 @@ namespace Commmunity.AspNetCore.ExceptionHandling.NewtonsoftJson
1111
{
1212
public static class PolicyBuilderExtensions
1313
{
14+
/// <summary>
15+
/// Write serialized object to response using <see cref="JsonSerializer"/> and pass control to next handler.
16+
/// </summary>
17+
/// <typeparam name="TException">
18+
/// The exception type
19+
/// </typeparam>
20+
/// <typeparam name="TObject">
21+
/// The result object type.
22+
/// </typeparam>
23+
/// <param name="builder">
24+
/// The policy builder
25+
/// </param>
26+
/// <param name="valueFactory">
27+
/// The result object factory.
28+
/// </param>
29+
/// <param name="settings">
30+
/// The JSON serializer settings <see cref="JsonSerializerSettings"/>.
31+
/// </param>
32+
/// <param name="index" optional="true">
33+
/// Handler index in the chain. Optional. By default handler added to the end of chain.
34+
/// </param>
35+
/// <returns>
36+
/// Policy builder
37+
/// </returns>
1438
[Obsolete("In case of using netcore2.1+ use Commmunity.AspNetCore.ExceptionHandling.Mvc instead")]
1539
public static IResponseHandlers<TException> WithBodyJson<TException, TObject>(
16-
this IResponseHandlers<TException> builder, Func<HttpRequest, TException, TObject> value, JsonSerializerSettings settings = null, int index = -1)
40+
this IResponseHandlers<TException> builder, Func<HttpRequest, TException, TObject> valueFactory, JsonSerializerSettings settings = null, int index = -1)
1741
where TException : Exception
1842
{
1943
return builder.WithBody((request, streamWriter, exception) =>
@@ -36,7 +60,7 @@ public static IResponseHandlers<TException> WithBodyJson<TException, TObject>(
3660
headers[HeaderNames.ContentType] = "application/json";
3761
}
3862

39-
TObject val = value(request, exception);
63+
TObject val = valueFactory(request, exception);
4064

4165
//return Task.CompletedTask;
4266
using (MemoryStream ms = new MemoryStream())

Commmunity.AspNetCore.ExceptionHandling/Logs/LogExceptionHandler.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@ public LogExceptionHandler(IOptions<LogHandlerOptions<TException>> settings)
2323
}
2424
public Task<HandlerResult> Handle(HttpContext httpContext, Exception exception)
2525
{
26-
if (exception.Data.Contains(DisableLoggingHandler.DisableLoggingFlagKey))
26+
var logLevel = this.Settings.Level?.Invoke(httpContext, exception) ?? LogLevel.Error;
27+
28+
if (logLevel == LogLevel.None)
2729
{
2830
return Task.FromResult(HandlerResult.NextHandler);
2931
}
3032

31-
ILoggerFactory loggerFactory =
32-
httpContext.RequestServices.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
33+
if (exception.Data.Contains(DisableLoggingHandler.DisableLoggingFlagKey))
34+
{
35+
return Task.FromResult(HandlerResult.NextHandler);
36+
}
3337

34-
if (loggerFactory != null)
38+
if (httpContext.RequestServices.GetService(typeof(ILoggerFactory)) is ILoggerFactory loggerFactory)
3539
{
36-
ILogger logger = loggerFactory.CreateLogger(this.Settings.Category ?? Const.Category);
40+
ILogger logger =
41+
loggerFactory.CreateLogger(this.Settings.Category?.Invoke(httpContext, exception) ??
42+
Const.Category);
3743

3844
EventId eventId = this.Settings.EventIdFactory != null
3945
? this.Settings.EventIdFactory(httpContext, exception)
@@ -45,7 +51,7 @@ public Task<HandlerResult> Handle(HttpContext httpContext, Exception exception)
4551

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

48-
logger.Log(this.Settings.Level, eventId, state, exception, formatter);
54+
logger.Log(logLevel, eventId, state, exception, formatter);
4955
}
5056

5157
return Task.FromResult(HandlerResult.NextHandler);
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
11
using System;
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Logging.Internal;
45
using Microsoft.Extensions.Options;
56

67
namespace Commmunity.AspNetCore.ExceptionHandling.Logs
78
{
9+
/// <summary>
10+
/// The log handler options
11+
/// </summary>
12+
/// <typeparam name="TException">
13+
/// The exception type
14+
/// </typeparam>
815
public class LogHandlerOptions<TException> : IOptions<LogHandlerOptions<TException>>
916
where TException : Exception
1017
{
1118
public LogHandlerOptions<TException> Value => this;
1219

20+
/// <summary>
21+
/// Factory for <see cref="EventId"/>
22+
/// </summary>
1323
public Func<HttpContext, Exception, EventId> EventIdFactory { get; set; }
1424

25+
/// <summary>
26+
/// The Formatter. By default state.ToString().
27+
/// </summary>
1528
public Func<object, Exception, string> Formatter { get; set; }
1629

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

19-
public string Category { get; set; }
20-
public LogLevel Level { get; set; } = LogLevel.Error;
35+
/// <summary>
36+
/// Factory for log category. By default "Commmunity.AspNetCore.ExceptionHandling" will be used.
37+
/// </summary>
38+
public Func<HttpContext,Exception,string> Category { get; set; }
39+
40+
/// <summary>
41+
/// 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.
42+
/// </summary>
43+
public Func<HttpContext, Exception, LogLevel> Level { get; set; }
2144
}
2245
}

0 commit comments

Comments
 (0)