I need to develop custom logger fro ASP.NET Core WEB API. I'd like to use the standard logging infrastructure, however, what I need is little bit different, because log records shouldn't be written to the destination media immediately, but I need
- create new logger per request
- collect log records of this request in memory
- save all records of the request as one record to the database at the end of that action or when the exception appears.
I'm quite new to ASP.NET Core, so I followed several tutorials, created the DBLogger (implementing ILogger and saving records to internal StringBuilder + one additional method Save), configuration, provider, extensions, I registered it in Startup and it works, but without the last step, which is the saving to the database. In the controller, I use the DI to get the logger, so it looks like this
public class ValuesController : Controller
{
private readonly ILogger<ValuesController> _logger;
public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<string> Get()
{
_logger.LogWarning("TestWarning");
_logger.LogInformation("TestInfo");
return new string[] { "value1", "value2" };
}
So I can now write log records, but how can I call the additional (custom) functionality of a specific Logger (in this case Save() method of my DBLogger)? The _logger property in my controller holds references to all loggers used in application internally, but the collection is not accessible from code.
The question, in general, could even be: How can I use the custom functionality of custom ILogger in the code. Or maybe - how can I get the reference to the custom logger?
[Edit] This is not my type (DBlogger), what i receive by DI, but i get the Microsoft.Extensions.Logging.Logger, containing non public property _logger of type Microsoft.Extensions.Logging.Logger containing non public collection of loggers containing my logger (see pic bellow). So that's why I feel it complicated
![1]](https://i.stack.imgur.com/1UoQo.jpg)
Thanks
Filip