I have created a ASP.Net Core 5 WebApi. It comes with the out of the box logging capabilities.
I have two questions.
- After I reference it in the class should I add try and catch block to capture and log the error or error will log itself?
- where can I see the logs. Like in ASP.NET Classics we would define the file in webconfig file and would be able to read the error or information in defined file? I would be deploying this app in Azure app service.
2
Answers
Unhandled exceptions will be logged by the framework.
There probably is a line somewhere in your Program.cs file like this
Host.CreateDefaultBuilder(args)
.According to the docs this will perform the following action
There is a lot of documentation arround logging. You could setup logging to write to a 3rd party system like nlog of log4net, see the docs , or write to a built-in provider, see these docs.
For web apps ,Azure Application Insights is a good choice, or to the Azure App Service Logs.
You could use System.IO to write to a text file. For example, something like this –