Reputation: 1941
I've added logging to an asp.net core application, using the excellent Serilog library. I have added sinks for both Sql Server and Literate console.
The sql server sink works perfectly of course, but, well this is a little embarrassing, I have no clue how to display the logs in a console window.
This is what ive tried:
Here's the code:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
ConfigureLogging();
}
private void ConfigureLogging()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.LiterateConsole(LogEventLevel.Verbose)
.WriteTo.RollingFile(".\\Logs\\log-{Date}.txt")
.WriteTo.MSSqlServer(Configuration.GetConnectionString("Logging"), "Logs"/*, columnOptions: new ColumnOptions()*/)
.CreateLogger();
}
public IConfigurationRoot Configuration { get; }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
app.Use(async (context, next) =>
{
await next();
// If there's no available file and the request doesn't contain an extension, we're probably trying to access a page.
// Rewrite request to use app root
if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
{
context.Request.Path = "/"; // Put your Angular root page here
await next();
}
});
loggerFactory
.AddSerilog();
// Ensure any buffered events are sent at shutdown
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseDefaultFiles();
app.UseStaticFiles();
//app.UseCors(builder => builder.AllowAnyOrigin());
app.UseMvc();
//app.UseHangfireDashboard(); // Will be available under http://localhost:5000/hangfire
//app.UseHangfireServer();
}
How do I display the logs in the console window?
Upvotes: 8
Views: 5872
Reputation: 31787
Under IIS the console logs aren't collected. You'll need to use one of the other sinks for an IIS hosted app, such as the rolling file you've configured.
Upvotes: 8