Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging exceptions in asp.net core / kestrel

The docs show how to set a page for when an exception occurs: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling

But how do I record the details of the exception in a log file on my server?

Does kestrel log exceptions and errors anywhere by default or do I have to do this manually?

Are there any examples or documentation available?

like image 738
Guerrilla Avatar asked Mar 30 '17 17:03

Guerrilla


1 Answers

Yes, ASP.NET core has the built-in logging, but it does not provide the ability to log to file directly. Instead, you need to use 3-rd party libraries. The good news is that all three most common logging libraries are available already for .NET Core as nuget packages: NLog, Serilog and log4net.

This article has a good overview over them and provides how-to-use examples: ASP.NET Core Logging Tutorial – What Still Works and What Changed?.


Then I suggest looking into samples in aspnet/Diagnostics repo. It shows how to custom or built-in error Middleware, ExceptionHandler.

like image 87
Set Avatar answered Nov 03 '22 01:11

Set