Ilogger getCurrentClassLogger在ActionFilterAttribute中仅适用于构造函数
我想在我的API中记录身体以获取某些请求,并为此目的创建了一个ActionFilterAttribute
。因此,我创建了一个简单的typeFilterAttribute
,它在其中调用了我的ActionFilterAttribute
。本地所有内容都可以正常工作,但是当我部署它时,除非我将ilogger
在构造函数中进行调用,否则什么都没有记录。正确,我需要在构造器中使用ilogger
在requestLoggerService
中,一切都起作用。
为什么我需要在构造函数中调用ilogger
以登录工作?
我故意不想使用ilogger
作为静态变量类似:
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
原因是UnitSests中ilogger
的简化过载。
简化,我的代码看起来像这样:
控制器的示例:
[NoAuthorization]
[HttpPost("response")]
[RequestLogger(true, true)]
public IActionResult PostResponse([FromBody] StringRequestModel model)
{
return Ok($"Received Message: '{model.Message}' at {DateTime.UtcNow.ToShortDateString()} - {DateTime.UtcNow.ToLongTimeString()} UTC");
}
我的requestloggerattribute
:
public class RequestLoggerAttribute : TypeFilterAttribute
{
public RequestLoggerAttribute(bool logBody = false, bool logHeader = false) : base(typeof(RequestLoggerService))
{
Arguments = new object[] { logBody, logHeader };
}
}
我的request> requestloggerservice
:
public class RequestLoggerService : ActionFilterAttribute
{
private readonly bool _logHeader;
private readonly bool _logBody;
public ILogger _logger = LogManager.GetCurrentClassLogger();
public RequestLoggerService(bool logBody, bool logHeader)
{
_logHeader = logHeader;
_logBody = logBody;
// Why do I need this line?
_logger.Info($"{nameof(RequestLoggerService)} called.");
}
public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
// log HttpContext header
_logger.Info($"requestHeaders: {requestHeadersText}");
// log HttpContext body
_logger.Info($"requestBody: {requestBodyText}");
}
}
I would like to log the body for some requests in my API and have created an ActionFilterAttribute
for this purpose. Therefore, I created a simple TypeFilterAttribute
which calls my ActionFilterAttribute
in it. Locally everything works fine, but when I deploy it nothing is logged unless I call the ILogger
once in the constructor. Correct, I need to use the ILogger
once in the RequestLoggerService
in the constructor, then everything works.
Why do I need to call the ILogger
in the constructor for logging to work?
I deliberately do not want to use the ILogger
as a static variable like:
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
The reason is the simplified overloading of the ILogger
in UnitTests.
Simplified, my code looks like this:
Example of controller:
[NoAuthorization]
[HttpPost("response")]
[RequestLogger(true, true)]
public IActionResult PostResponse([FromBody] StringRequestModel model)
{
return Ok(quot;Received Message: '{model.Message}' at {DateTime.UtcNow.ToShortDateString()} - {DateTime.UtcNow.ToLongTimeString()} UTC");
}
My RequestLoggerAttribute
:
public class RequestLoggerAttribute : TypeFilterAttribute
{
public RequestLoggerAttribute(bool logBody = false, bool logHeader = false) : base(typeof(RequestLoggerService))
{
Arguments = new object[] { logBody, logHeader };
}
}
My RequestLoggerService
:
public class RequestLoggerService : ActionFilterAttribute
{
private readonly bool _logHeader;
private readonly bool _logBody;
public ILogger _logger = LogManager.GetCurrentClassLogger();
public RequestLoggerService(bool logBody, bool logHeader)
{
_logHeader = logHeader;
_logBody = logBody;
// Why do I need this line?
_logger.Info(quot;{nameof(RequestLoggerService)} called.");
}
public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
// log HttpContext header
_logger.Info(quot;requestHeaders: {requestHeadersText}");
// log HttpContext body
_logger.Info(quot;requestBody: {requestBodyText}");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论