How to enable Application Logs in Azure for Net Co

2019-05-07 01:59发布

I am trying to enable application logs in azure. I have a dummy Net Core 2 App running in an appService in azure.

and basically my goal is to see the trace messages in the log stream and in the application log files but I have not found the right way to do this.

One of the challenge I have found reading other posts is that they assume a web config in place.

Home Controller Configuration In Azure Startup.cs

4条回答
一纸荒年 Trace。
2楼-- · 2019-05-07 02:43

Run dotnet add package EntityFramework Microsoft.Extensions.Logging.AzureAppServices to install logging extension to your project.

Program.cs file for reference:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
             .ConfigureLogging((hostingContext, logging) =>
             {
                 logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                 logging.AddConsole();
                 logging.AddDebug();
                 logging.AddAzureWebAppDiagnostics();
             })
            .UseApplicationInsights()
            .UseStartup<Startup>()
            .Build();
}
查看更多
叼着烟拽天下
3楼-- · 2019-05-07 02:44

You could get answer from this blog. The following is the snippet from the blog.

Setting up logging in an ASP.NET Core app doesn’t require much code. ASP.NET Core new project templates already setup some basic logging providers with this code in the Startup.Configure method:

loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
loggerFactory.AddDebug();

enter image description here

查看更多
Anthone
4楼-- · 2019-05-07 02:55

You need to use "Microsoft.Extensions.Logging.AzureAppServices" package and then register the logging provider for azure using code below.

 loggerFactory.AddAzureWebAppDiagnostics( 
    new AzureAppServicesDiagnosticsSettings 
    {
          OutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level}] {RequestId}-{SourceContext}: {Message}{NewLine}{Exception}" 
    } 
  );
查看更多
别忘想泡老子
5楼-- · 2019-05-07 02:57

The documentation for ASP.NET Core 2.2 is here.

Firstly, enable Application Logging and choose the appropriate level:

switch on application logging

This may be all you need to do to diagnose any problems. But if you want log messages and see them, install the Microsoft.Extensions.Logging.AzureAppServices NuGet package.

Then, configure logging:

using Microsoft.Extensions.Logging;

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
    .ConfigureLogging(logging =>
    {
        logging.AddAzureWebAppDiagnostics();
    })
    .UseStartup<Startup>();

Now you can inject and use ILogger:

public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
    Configuration = configuration;
    this.logger = logger;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    logger.LogWarning("Starting up");

Then in the Azure App Service, click on Log stream: Log stream

查看更多
登录 后发表回答