I’ve been upgrading several projects and I’ve noticed that the recommended logging solution with .NET core is to use its own logging abstraction rather than the ServiceStack logging.
My projects used to use Serilog through the ServiceStack.Logging but I’d like to tidy up my code and get it up to date since I’ve made the move to NET 6.0 (great job by the way!)
My question is what’s the best way to achieve console logging with a selfhost ServiceStack project, I seem to be getting confused between the NET Core and ServiceStack logging solutions especially with the old code making things confusing.
My Program.cs has the following startup code:
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.MinimumLevel.Is(LogEventLevel.Debug)
.CreateLogger();
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
ApplicationName = typeof(Program).Assembly.FullName,
ContentRootPath = Directory.GetCurrentDirectory(),
EnvironmentName = Debugger.IsAttached ? Environments.Development : Environments.Production,
WebRootPath = "wwwroot"
});
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
builder.Host.UseSerilog();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
app.UseHttpsRedirection();
}
else
{
app.UseDeveloperExceptionPage();
}
app.Run();
When I’m attempting to log in my AppHost startup code I’m using:
var loggerFactory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
var _logger = loggerFactory.CreateLogger<AppHost>();
_logger.LogInformation("test");
I’m assuming from reading that I’ll need to add the following code in my service class:
private readonly ILogger<ApiService> _logger;
public ApiService(ILogger<ApiService> logger) => _logger = logger;
Does any of that make sense? Thanks for any help.