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?
You have literally declared that logs will go to four potential destinations: App Insights, the console, the debug output, and the event viewer.
Regardless, you can still see the console output in the Output pane in Visual Studio. There's a drop down there called "Show output from" and one of the choices is "ASP.NET Core Web server". Select this and you'll see all the console output.
ASP.NET uses IIS logging, so it's really an IIS question. Though there is some detailed info in Event Viewer for some types of events. In IIS6 (and prior), this is located in %SystemRoot%\system32\logfiles , and in IIS7, this is located in %SystemDrive%\inetpub\logs\LogFiles .
\%home%\LogFiles\stdout .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With