我正在为我的 .NET Core 应用程序设置一个自定义中间件来记录异常错误,并在 Startup.cs 中有以下内容来注册我的上下文:
services.AddHttpContextAccessor();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
在我的配置中,我添加了以下内容以使用自定义中间件:
app.UseMiddleware<CustomMiddleware>();
我的自定义中间件类如下:
public class CustomMiddleware
{
private readonly RequestDelegate next;
public CustomMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
this.BeginInvoke(context);
await this.next.Invoke(context);
this.EndInvoke(context);
}
catch(Exception ex)
{
//Capture the exception.
string hostName = Environment.MachineName;
string url = StringFunctions.getCurrentUrl(context);
string userName = string.Empty;
string controllerName = string.Empty;
string actionName = string.Empty;
//THIS IS NULL BELOW.
IActionContextAccessor contextAccessor = context.RequestServices.GetService(typeof(IActionContextAccessor)) as IActionContextAccessor;
RouteData routeData = contextAccessor.ActionContext.RouteData;
if (context.User != null)
{
userName = context.User.Identity.Name;
}
if (routeData != null && routeData.Values != null)
{
controllerName = routeData.Values["controller"].ToString();
actionName = routeData.Values["action"].ToString();
}
EventLogging.LogApplicationError(hostName, controllerName, actionName, url, userName, ex);
}
}
private void BeginInvoke(HttpContext context)
{
//custom work here
}
private void EndInvoke(HttpContext context)
{
// Do custom work after controller execution
}
}
我试图从中获取 ActionContext.RouteData 值的“contextAccessor”似乎是 Null。
我究竟做错了什么?谢谢你。