Simple ASP.NET Core Web API using Serilog to log to Azure Application Insights and App Service Log Stream
The .NET Core Web API in this repository uses ILogger
of NuGet package Microsoft.Extensions.Logging
. ILogger<T>
is injected into the constructor of the class that needs to log.
The underlying logging framework is Serilog. Serilog is configured in Program.cs
and appsettings.json
(appsettings.Development.json
for local development environment) of the application.
Serilog is configured so that the application logs to the following sinks:
- Startup logs are logged to the console
- Any other application logs are logged ...
- ... to the console, if the application runs in local development environment (i.e. when started in
Visual Studio
orVisual Studio Code
whereASPNETCORE_ENVIRONMENT
is by default set toDevelopment
) - ... to Azure Application Insights, if the the application runs in Azure on an Azure App Service (where
ASPNETCORE_ENVIRONMENT
is by default set toProduction
, thereforeappsettings.Development.json
is ignored) - ... to a log file, if the application runs in Azure on an Azure App Service to allow viewing near-realtime logs in Azure App Service
Log stream
- ... to the console, if the application runs in local development environment (i.e. when started in
Important
In this sample Serilog is configured for Linux Azure App Services
Serilog creates traces in Application Insights (TraceTelemetry
) for log levels defined in appsettings.json
MinimumLevel
. However, if the log event contains any exceptions it will always be sent as ExceptionTelemetry
to Application Insights.
To make Serilog sending custom application logs of log level Debug
to Application Insights, the following application setting has to be added to the Azure App Service (see Settings
> Configuration
of App Service):
- Name:
Serilog__MinimumLevel__Default
- Value:
Debug
Important
This setting should only be added to the Azure App Service in case of troubleshooting. Otherwise, the logs will be flooded with debug logs.
The NuGet package Serilog.AspNetCore includes middleware for request logging which can be configured to make logs and exceptions from .NET core middleware visible in the logs.
For more details see here
To query the application logs in Azure Application Insights, proceed as follows:
- Log in to the Azure Portal
- Switch to the corresponding directory (Azure tenant)
- Search for
Application Insights
in the search bar on the top - Select the application insights resource the app sends its logs to
- Navigate to
Logs
in sectionMonitoring
in the menu on the left - Enter one of the following queries in the query editor and click
Run
:- To query non exception logs
traces | where message startswith "START_OF_LOG_MESSAGE"
- To query exception logs
exceptions
- To query non exception logs
Important
Make sure you check the whole stream for your logs as they seem to not be ordered by date but by log file
To stream the application logs in near-realtime, proceed as follows:
- Log in to the Azure Portal
- Switch to the corresponding directory (Azure tenant)
- Search for
App Service
in the search bar on the top - Select the app service resource the app runs on
- Navigate to
Log stream
in sectionMonitoring
in the menu on the left
- Azure tenant
- Azure subscription
-
Create a new resource group (i.e.
aspnetcore-serilog-azureappservice
) -
Create a new workspace-based application insights resource in the before created resource group (i.e.
aspnetcore-serilog-azureappservice-appinsights
) -
Create a new app service plan in the before created resource group (i.e.
aspnetcore-serilog-azureappservice-appplan
)- Operating system:
Linux
- Pricing tier:
F1
- Operating system:
-
Create a new web app (i.e.
aspnetcore-serilog-azureappservice-app
)- Publish:
Code
- Runtime stack:
.NET 8 (LTS)
- Operating system:
Linux
- Linux Plan: select before created app service plan
- Enable and configure continuous deployment under
Deployment
tab
- Publish:
-
Ensure the following application setting is set in the Azure App Service:
This setting is already set, if deployment is done using GitHub Actions integration.
- Name:
APPLICATIONINSIGHTS_CONNECTION_STRING
- Value: connection string of the before created application insights resource
- Name:
Important
Without this setting, the application will not log to Application Insights.
After successful deployment, the weather forecast API endpoint is available under APP_SERVICE_DEFAULT_DOMAIN/WeatherForecast