Ilogger getCurrentClassLogger在ActionFilterAttribute中仅适用于构造函数

发布于 2025-02-05 05:46:43 字数 2225 浏览 2 评论 0原文

我想在我的API中记录身体以获取某些请求,并为此目的创建了一个ActionFilterAttribute。因此,我创建了一个简单的typeFilterAttribute,它在其中调用了我的ActionFilterAttribute。本地所有内容都可以正常工作,但是当我部署它时,除非我将ilogger在构造函数中进行调用,否则什么都没有记录。正确,我需要在构造器中使用iloggerrequestLoggerService中,一切都起作用。

为什么我需要在构造函数中调用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文