I created a ASP.NET 6, Blazor Server-side project. I injected a logger to the WeatherForecastService
classes. I created a library project, and referenced it from the ASP.NET project. I created a object in that library in the ASP.NET project and passed a logger.
If I start it in debug mode in Visual Studio, the log messages from both projects are printed in the Output panel. That is good, but what I what I want to do is, in addition to that (that is, not disabling the log output in the Output panel of VS), show the logs within my ASP.NET project. For example, there can be a "Logs" page.
Probably there is no easy way to send log messages to the client browser in real-time, so I am going to poll the server at every second for new log messages. To do that, I think I have to get notified whenever a log message happens in the ASP.NET project. Not just for the logs from the ASP.NET project itself, but from the referenced project, too, just like VS's Output panel. Can I do that?
ASP.NET 6, Blazor Server-side project
namespace BlazorApp1.Data
{
public class WeatherForecastService
{
private readonly ILogger _logger;
public WeatherForecastService(ILogger<WeatherForecastService> logger, IServiceProvider sp)
{
_logger = logger;
var d = new Dog(sp.GetRequiredService<ILogger<Dog>>());
logger.LogInformation("WeatherForecastService created.");
}
}
Referenced "library" project
using Microsoft.Extensions.Logging;
namespace ClassLibrary1
{
public class Dog
{
public Dog(ILogger<Dog> logger)
{
logger.LogInformation("Dog created.");
}
}
}
CodePudding user response:
Depending on where are you logging you are ether pooling the file or the database. For referenced projects and logs in them you need to implement logger and actually log data to the same file. or different one, but in service you need to fetch all data from all the files. As far as i know default logger that is logging to console is just for that. You need to ether implement your own logging library or you can import serilog or log4net. And insted of using default ILogger in the same way you use your implementation witch logs data to ether file or db. Microsoft link for implementing logging provider https://learn.microsoft.com/en-us/dotnet/core/extensions/custom-logging-provider