简体   繁体   English

为 api 控制器自动记录消息

[英]automating logging messages for api controllers

I have the following controller method in my Entity Framework Core project.我的 Entity Framework Core 项目中有以下控制器方法。

I just started learning about .NetCore logging and I love it!我刚刚开始学习 .NetCore 日志记录,我喜欢它!

However I have a feeling it might become difficult to attach logging to all of my controllers.但是我有一种感觉,将日志记录附加到我的所有控制器可能会变得困难。

Currently my project has quite a few controllers for various endpoints.目前我的项目有很多用于各种端点的控制器。

I started adding logging code, and I fear that I may be making it more difficult than needs to be.我开始添加日志记录代码,我担心我可能会使它变得比需要的更困难。

So I inject the logger, then I create a few strings like Message , Controller , and ErrorMessage .所以我注入了记录器,然后我创建了一些字符串,比如MessageControllerErrorMessage

I then assign values to those inside the controller method as needed.然后我根据需要将值分配给控制器方法中的值。

I was wondering if there was a way to do this in a more automated way instead of manually all that to every controller method.我想知道是否有一种方法可以以更自动化的方式执行此操作,而不是手动将所有这些操作到每个控制器方法。

Unfortunately I can't create a base class because my controllers need to use ControllerBase .不幸的是,我无法创建基类,因为我的控制器需要使用ControllerBase

[Route("api/[controller]")]
[ApiController]
public class PetrolStationController : ControllerBase
{
    private readonly petrol_ProdContext _context;
    private readonly ILogger _logger;

    public PetrolStationController(petrol_ProdContext context, ILogger<PetrolStationController> logger)
    {
        _context = context;
        _stationService = stationService;
        _logger = logger;
    }

    public string Message { get; set; }
    public string Controller { get; set; }
    public string ErrorMessage { get; set; }

    [HttpGet("GetStationNodeObject/{stationId}")]
    public async Task<ActionResult<StationList>> GetStationLocation(Guid stationID)
    {
        Controller = this.GetType().Name.ToString();

        var stationList = await _context.StationList.FindAsync(stationID);

        if (stationList == null)
        {
            ErrorMessage = "stationID does not exist";
            Message = $"Endpoint: {Controller}: {DateTime.UtcNow.ToLongTimeString()} => {ErrorMessage}";
            _logger.LogInformation(Message);
            return NotFound();
        }

        var petrolStation = await _stationService.GetStationLocation(stationID);
        Message = $"Endpoint: {Controller}: {DateTime.UtcNow.ToLongTimeString()}, Success!";
        _logger.LogInformation(Message);

        return petrolStation;
    }
}

I was hoping someone could provide some insight for a better way of doing this.我希望有人可以提供一些洞察力来更好地做到这一点。

thank you all谢谢你们

Check this tutorial: https://nblumhardt.com/2019/10/serilog-in-aspnetcore-3/检查本教程: https : //nblumhardt.com/2019/10/serilog-in-aspnetcore-3/

I am using this approach quit often.我经常使用这种方法退出。 This will log all exceptions automatically and if you want more you can easily add it inside your controller.这将自动记录所有异常,如果您想要更多,您可以轻松地将它添加到您的控制器中。 Biggest PRO is that you set up everything at one place and you get uniform log format.最大的优点是您将所有内容都设置在一个地方,并获得统一的日志格式。 You can use log server or file to save logs.您可以使用日志服务器或文件来保存日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM