-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathApiRequestContextExtensions.cs
107 lines (98 loc) · 4.05 KB
/
ApiRequestContextExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace WebApiClientCore
{
/// <summary>
/// 提供ApiRequestContext的扩展
/// </summary>
public static class ApiRequestContextExtensions
{
/// <summary>
/// 尝试根据参数名获取参数值
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="context"></param>
/// <param name="parameterName">参数名</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool TryGetArgument<TValue>(this ApiRequestContext context, string parameterName, [MaybeNullWhen(false)] out TValue value)
{
return context.TryGetArgument(parameterName, StringComparer.Ordinal, out value);
}
/// <summary>
/// 尝试根据参数名获取参数值
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="context"></param>
/// <param name="parameterName">参数名</param>
/// <param name="nameComparer">比较器</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool TryGetArgument<TValue>(this ApiRequestContext context, string parameterName, StringComparer nameComparer, [MaybeNullWhen(false)] out TValue value)
{
if (context.TryGetArgument(parameterName, nameComparer, out var objValue) && objValue is TValue tValue)
{
value = tValue;
return true;
}
value = default;
return false;
}
/// <summary>
/// 尝试根据参数名获取参数值
/// </summary>
/// <param name="context"></param>
/// <param name="parameterName">参数名</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool TryGetArgument(this ApiRequestContext context, string parameterName, out object? value)
{
return context.TryGetArgument(parameterName, StringComparer.Ordinal, out value);
}
/// <summary>
/// 尝试根据参数名获取参数值
/// </summary>
/// <param name="context"></param>
/// <param name="parameterName">参数名</param>
/// <param name="nameComparer">比较器</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool TryGetArgument(this ApiRequestContext context, string parameterName, StringComparer nameComparer, out object? value)
{
foreach (var parameter in context.ActionDescriptor.Parameters)
{
if (nameComparer.Equals(parameter.Name, parameterName))
{
value = context.Arguments[parameter.Index];
return true;
}
}
value = null;
return false;
}
/// <summary>
/// 获取以 ApiAction 为容器的 ILogger
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static ILogger? GetActionLogger(this ApiRequestContext context)
{
return context.ActionDescriptor.Properties.GetOrAdd(typeof(ILogger), CreateLogger) as ILogger;
object? CreateLogger(object _)
{
var loggerFactory = context.HttpContext.ServiceProvider.GetService<ILoggerFactory>();
if (loggerFactory == null)
{
return null;
}
var action = context.ActionDescriptor;
var parameters = action.Parameters.Select(item => HttpApi.GetName(item.ParameterType, includeNamespace: false));
var categoryName = $"{action.InterfaceType.FullName}.{action.Member.Name}({string.Join(", ", parameters)})";
return loggerFactory.CreateLogger(categoryName);
}
}
}
}