[英]How to use ILogger without dependency injection?
凈核心項目。 我正在使用 azure application insights 來記錄。 我在 services.AddApplicationInsightsTelemetry(); 中添加了以下行; 在我的 startup.cs 中。 在controller和業務層我會通過依賴注入獲取ILogger。 但我想到了一些不同的東西。 我創建了 ApiExceptionFilter,代碼如下所示。
public class ApiExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
ApiError apiError = null;
if (context.Exception is ApiException)
{
// handle explicit 'known' API errors
var ex = context.Exception as ApiException;
context.Exception = null;
apiError = new ApiError(ex.Message);
context.HttpContext.Response.StatusCode = ex.StatusCode;
}
else if (context.Exception is UnauthorizedAccessException)
{
apiError = new ApiError("Unauthorized Access");
context.HttpContext.Response.StatusCode = 401;
// handle logging here
}
else if (context.Exception is NotImplementedException)
{
apiError = new ApiError("Not Implemented");
context.HttpContext.Response.StatusCode = 401;
// handle logging here
}
else if (context.Exception is ArgumentNullException)
{
apiError = new ApiError(string.Format("Parameter: {0} is required, it cannot be empty", (context.Exception as ArgumentNullException).ParamName));
context.HttpContext.Response.StatusCode = 401;
// handle logging here
}
else
{
// Unhandled errors
var msg = context.Exception.GetBaseException().Message;
string stack = context.Exception.StackTrace;
apiError = new ApiError(msg);
apiError.detail = stack;
context.HttpContext.Response.StatusCode = 500;
// handle logging here
}
// always return a JSON result
context.Result = new JsonResult(apiError);
base.OnException(context);
}
}
但是在這里我不能做類似的事情
private readonly ILogger _logger;
public ApiExceptionFilter(ILogger logger)
{
_logger = logger;
}
如果我進行上述更改
[ApiController]
[ApiExceptionFilter] //here it asking for paramter ilogger
public class MyController : ControllerBase
在上面的代碼中 [ApiExceptionFilter] 它會要求我傳遞 ILogger 參數。
我只是想知道在 ApiExceptionFilter 中添加 ILogger 的正確方法是什么。 有人可以幫助我嗎? 任何幫助,將不勝感激。 謝謝
您可以添加異常過濾器:
[TypeFilter(typeof(ApiExceptionFilter))]
這將允許您的過濾器使用來自 DI 容器的服務,因為框架將在請求時創建過濾器。
例如
public class ApiExceptionFilter : ExceptionFilterAttribute
{
public ApiExceptionFilter(ILogger logger)
{
// Assign the logger to a field etc.
}
}
您還可以使用[ServiceFilter(typeof(ApiExceptionFilter))]
並將過濾器本身注冊到 DI 容器中。
為了在操作過濾器中使用 DependencyInjection,您需要使用 TypeFilterAttribute,
例子:
public class HandleExceptionAttribute : TypeFilterAttribute
{
public HandleExceptionAttribute() : base(typeof(HandleExceptionPrivate))
{
}
class HandleExceptionPrivate : ExceptionFilterAttribute
{
private readonly ILogger _logger;
private readonly IHostingEnvironment _env;
public HandleExceptionPrivate(ILoggerFactory logger, IHostingEnvironment env)
{
_logger = logger.CreateLogger("Exeption Filter");
_env = env;
}
public override void OnException(ExceptionContext context)
{
_logger.LogError(Constants.LoggingEvents.EXCEPTION, context.Exception, "message");
context.ExceptionHandled = true;
context.Exception = null;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.