Search code examples
c#asp.net-mvcazureazure-web-app-service

How to Log Exceptions to Azure application logs


I have a ASP.Net MVC Web Application hosted in Azure Web Service. I need to log Exceptions to Application Log.

Later I want to check and review what kind of exceptions happened.

How can I do it?


Solution

  • If by exceptions you mean those caught inside a try-catch then you could do;

    1. Follow the steps mentioned here, you need to toggle this setting on your Azure web app;

      Your App > App Service Logs > Turn On Application Logging (FileSystem)

    2. Then use the code below; Trace.Information(), Trace.Warning(), Trace.Error()

    using System.Diagnostics;
    
    public ActionResult sample(){
       try{
          Trace.Information("Hello World!");
       }catch(exception e){
          Trace.Error(e.Message);
       }
    }