I am trying to use a very simple setup to log my Asp.Net core application events into Azure Analytics. I am using Windows App Service with NetCore 6.0. Everything is working fine, I can see my logs in the AppService Log Stream and Log(Analytics) AppServiceAppLogs table but when I use Scopes for logging like below code, these values are not showing in the logs.
using (logger.BeginScope(new Dictionary<string, object> { { "user" , "Alex" } }))
{
logger.LogInformation("Test Log");
}
I had to add below configuration in Program.cs to see the scopes in the LogStream
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
options.IncludeScopes = true;
});
but cannot get see them in the Azure Analytics workspace. Anyone has any experince with using scopes/strucutured logging with Azure Analytics and AspNet.Extesions.Logging ?
I tried using Serilog and I am getting nice structured josn log in console on my local machine but again no luck with Azure Analytrics and LogStream.
Thanks to
v-regandowner
for the code reference.You can use the
ApplicationInsightsLoggerProvider
to log scopes in Azure Analytics.The
ApplicationInsightsLoggerProvider
supports log scopes.Scopes are enabled by default. If the scope is of type
IReadOnlyCollection<KeyValuePair<string,object>>
, then each key/value pair in the collection is added to the Application Insights telemetry as custom properties.To use scopes/structured logging with Azure Analytics and
AspNet.Extensions.Logging
, you need to follow these steps:Microsoft.Extensions.Logging.ApplicationInsights
NuGet package to your project.ApplicationInsightsLoggerOptions
to include scopes.Use the below Packages.
Sample code
For further information refer to MSDoc1 and MSDoc2.