Bashir
Bashir

Reputation: 81

How to use Azure Web App and Microsoft Extension Logging to log Scopes in to Azure Analytics?

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.

Upvotes: 0

Views: 756

Answers (1)

Rajesh  Mopati
Rajesh Mopati

Reputation: 1512

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.

Log Scopes in Analytics using (_logger.BeginScope(new Dictionary<string, object> { ["

To use scopes/structured logging with Azure Analytics and AspNet.Extensions.Logging, you need to follow these steps:

  1. Add the Microsoft.Extensions.Logging.ApplicationInsights NuGet package to your project.
  2. Configure the ApplicationInsightsLoggerOptions to include scopes.
  3. Use the ILogger interface to log messages with scopes.

Use the below Packages.

 <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
  <PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.17.0"/>

Sample code

var services = new ServiceCollection();
services.AddLogging(builder =>
       {
            builder.AddApplicationInsights(options =>
                 {
                    options.IncludeScopes = true;
                 });
             builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>(
             typeof(Program).FullName, LogLevel.Information);
       });

     var serviceProvider = services.BuildServiceProvider();
     var logger = serviceProvider.GetRequiredService<ILogger<Program>>();

     using (logger.BeginScope(new Dictionary<string, object> { { "user", "username" } }))
         {
                logger.LogInformation("Test Log");
         }

enter image description here

For further information refer to MSDoc1 and MSDoc2.

Upvotes: 0

Related Questions