Continuing of our previous article here, let’s explore how to show the logs in Azure Log Stream.
Integrating Serilog
To begin, incorporate Serilog into your project using Visual Studio or the .NET CLI:
# Using Visual Studio
Install-Package Serilog.AspNetCore
# Using .NET CLI
dotnet add package Serilog.AspNetCore
Configuring Serilog
Add the necessary configuration settings in your appsettings.json
file, resembling the structure below:
"Serilog": {
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.File"],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "Logs/applog-.txt",
"rollingInterval": "Day"
}
}
],
"Enrich": ["FromLogContext", "WithMachineName"],
"Properties": {
"ApplicationName": "Your ASP.NET Core App"
}
}
Deploying Your App to Azure
Deploy your application to Azure using the steps outlined in our previous article:
dotnet publish -o publish
cd publish; zip -r ../publish.zip *; cd ..
az webapp deployment source config-zip --src publish.zip -n as-20231023 -g test-rg
Configuring Log File Path for Azure Log Stream
To display logs in Azure Log Stream, modify the Serilog file path using a key such as Serilog__WriteTo__1__Args__path
. Set it to something like D:\home\LogFiles\Application\applog-.txt
.
Update the Serilog file configuration as follows:
{
"Name": "File",
"Args": {
"path": "Logs/applog-.txt",
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}",
"shared": "true",
"flushToDiskInterval": "0:00:02"
}
}
The important settings are flushToDiskInterval
and shared
.
Monitoring Logs in Azure Log Stream
If all configurations were successful and application logs in Azure were enabled, restart the web app to observe the logs in Azure Log Stream.
Documentation and links:
- Stackoverflow question - Use Serilog with Azure Log Stream - https://stackoverflow.com/questions/49744852/use-serilog-with-azure-log-stream
- Configuring Serilog through appsettings.json file - https://mohsen.es/configuring-serilog-through-appsettings-json-file-33b26594bb46