Logging .net core web api with azure

Tanul 1,251 Reputation points
2020-12-26T20:39:57.45+00:00

Team,

I need to host a .net core web api as app service in azure. Is it mandatory to enable file system option to view console logs. This option will any way work for 12 hours and not recommended in production then how could we see our logs. I have used only console log provider in my application. Is there any way to view logs on some console rather than saving in the file.

How can we get the console like in linux based systems or kubernetes.

Thank you

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,119 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,865 questions
{count} vote

Accepted answer
  1. ajkuma 22,401 Reputation points Microsoft Employee
    2020-12-29T13:22:04.967+00:00

    @Tanul , With the curl itself, you can scope the live traces to any providers/folders, you may additional specify the path.

    Kudu service (as well as its application) write traces to the /Logfiles folder. The logstream endpoint (/logstream in the Kudu service) enables you to view with any real time traces as they are being written.

    /api/logstream/kudu/trace => kudu related traces intended for diagnostic kudu issues.
    /api/logstream/kudu/deployment => kudu deployment traces intended for application developers/operators.
    /api/logstream/application => application traces.
    /api/logstream/http => iis logs.

    Kindly see this Diagnostic Log Stream for more details.

    Yes, you can change DIAGNOSTICS_TEXTTRACETURNOFFPERIOD to a value greater than 12 hours (12 * 60 * 60 * 1000 in milliseconds) based on your requirement (considering the space).


4 additional answers

Sort by: Most helpful
  1. ajkuma 22,401 Reputation points Microsoft Employee
    2020-12-28T19:37:30.3+00:00

    @Tanul , Just to add more clarity

    Failure to disable the stdout log can lead to app or server failure. There's no limit on log file size or the number of log files created. So it is recommended to only use stdout logging to troubleshoot app startup problems and disable stdout logging when troubleshooting is complete by setting stdoutLogEnabled="false".

    For general logging in an ASP.NET Core app after startup, use a logging library that limits log file size and rotates logs. For more information, see third-party logging providers.
    We avoid any feature that involves writing to the app's file system in a continuous manner, which can degrade performance.

    Kindly refer Logging in .NET Core and ASP.NET Core doc for more details.


  2. ajkuma 22,401 Reputation points Microsoft Employee
    2020-12-28T19:45:44.593+00:00

    @Tanul ,

    You're limited to a maximum of 100 megabytes of logs when you use the file system. (File system logs are only for short-term retention. Azure deletes old log files to make room for new ones after the limit is reached.)

    In cases when configuring logs to go to File System/, they go to a different LogFiles folder: D:\home\LogFiles\SiteExtensions\Kudu. If you need to stream them, you can run (log in with your deployment credentials):

    curl https://{app}.scm.azurewebsites.net/api/logstream/SiteExtensions/Kudu -u YourDeploymentUser  
    

    Or if using Azure site log tail (which wraps the same API), pass -p SiteExtensions/Kudu to tell it to stream from that path.

    Timeout in milliseconds to keep application logging on (Default is 43200000, which is 12 hours) is on the app:
    https://github.com/projectkudu/kudu/wiki/Configurable-settings#diagnostics-related-settings

    0 comments No comments

  3. Tanul 1,251 Reputation points
    2020-12-29T11:45:21.54+00:00

    @ajkuma , Please correct me if I'm wrong. So we've 2 options here. Save the log files let say using any 3rd party logger in some folder for eg. D:\home\LogFiles\somedirectory\mylogs

    Now, if we need to access it then we can open kudu portal and check the logs manually.

    Secondly, if we need to use the azure log stream feature for STDOUT logs then firstly, we have to enable azure application log file system option. To increase that default 12 hours period we have to create this variable in app settings and specify the value.

    DIAGNOSTICS_TEXTTRACETURNOFFPERIOD

    And, once the time expires I have to manually enable it again.

    If we need to access stdout logs then we can fire the curl command like this or use log stream tab in azure(default place for logs is D:\home\LogFiles\SiteExtensions\Kudu)

     curl https://{app}.scm.azurewebsites.net/api/logstream/SiteExtensions/Kudu/ -u YourDeploymentUser  
    

    Am I correct here or mixed-up the steps. Could you please confirm.

    0 comments No comments

  4. JoyDutt 816 Reputation points
    2020-12-29T14:29:33.357+00:00

    Hi @Tanul

    You can try enabling logging features and later use Kudo to view logs.Some minor cost for using storage to store file but still you can always limit.

    Azure Monitor integration to route your logs to an Azure Storage Account or Log Analytics workspace. Persisting all logs on the file system indefinitely would fill your app’s file system eventually.

    https://azure.github.io/AppService/2019/11/01/App-Service-Integration-with-Azure-Monitor.html

    Kudo Steps :: https://learn.microsoft.com/en-us/learn/modules/capture-application-logs-app-service/
    http://sitecore.skowronski.it/sitecore/how-to-get-the-latest-logs-from-azure-with-kudu/
    https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#enable-web-server-logging

    (Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members. Thank You)

    Regards,
    J.D.

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    0 comments No comments