skip to Main Content

I have created a ASP.Net Core 5 WebApi. It comes with the out of the box logging capabilities.
I have two questions.

  1. 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?
  2. 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


  1. 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?

    Unhandled exceptions will be logged by the framework.

    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.

    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

    Configure the ILoggerFactory to log to the console, debug, and event source output.

    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.

    Login or Signup to reply.
  2. You could use System.IO to write to a text file. For example, something like this –

            // create file 
    
            string strPath = @"C:errors.txt";
    
            if (!File.Exists(strPath))
            {
                File.Create(strPath).Dispose();
            }
            try
            {
                // code
            }
            catch (ex)
            {
                using (StreamWriter sw = File.AppendText(strPath))
                {
                    sw.WriteLine(ex);
                }
    
                throw;
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search