C# 如何在Azure for Net Core 2应用程序中启用应用程序日志?
我正在尝试在azure中启用应用程序日志。 我有一个虚拟的NetCore2应用程序在azure的appService中运行 基本上,我的目标是在日志流和应用程序日志文件中查看跟踪消息,但我还没有找到正确的方法 我在阅读其他文章时发现的一个挑战是,他们假设有一个web配置C# 如何在Azure for Net Core 2应用程序中启用应用程序日志?,c#,azure,logging,asp.net-core-2.0,C#,Azure,Logging,Asp.net Core 2.0,我正在尝试在azure中启用应用程序日志。 我有一个虚拟的NetCore2应用程序在azure的appService中运行 基本上,我的目标是在日志流和应用程序日志文件中查看跟踪消息,但我还没有找到正确的方法 我在阅读其他文章时发现的一个挑战是,他们假设有一个web配置 你可以从中得到答案。以下是博客中的片段 在ASP.NET核心应用程序中设置日志记录不需要太多代码。ASP.NET核心新项目模板已在启动时使用此代码设置了一些基本日志提供程序。配置方法: 您需要使用“”包,然后使用下面的代码注册
你可以从中得到答案。以下是博客中的片段 在ASP.NET核心应用程序中设置日志记录不需要太多代码。ASP.NET核心新项目模板已在启动时使用此代码设置了一些基本日志提供程序。配置方法: 您需要使用“”包,然后使用下面的代码注册azure的日志记录提供程序
loggerFactory.AddAzureWebAppDiagnostics(
new AzureAppServicesDiagnosticsSettings
{
OutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level}] {RequestId}-{SourceContext}: {Message}{NewLine}{Exception}"
}
);
运行
dotnet添加包EntityFramework Microsoft.Extensions.Logging.AzureAppServices
将日志记录扩展安装到项目中
Program.cs文件供参考:
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();
}
公共类程序
{
公共静态void Main(字符串[]args)
{
BuildWebHost(args.Run();
}
公共静态IWebHost BuildWebHost(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext,logging)=>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection(“logging”));
logging.AddConsole();
logging.AddDebug();
logging.AddAzureWebAppDiagnostics();
})
.UseApplicationInsights()
.UseStartup()
.Build();
}
ASP.NET Core 2.2的文档如下所示
首先,启用应用程序日志记录并选择适当的级别:
这可能是诊断任何问题所需的全部操作。但是,如果希望记录并查看消息,请安装Microsoft.Extensions.Logging.AzureAppServices NuGet软件包
然后,配置日志记录:
using Microsoft.Extensions.Logging;
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddAzureWebAppDiagnostics();
})
.UseStartup<Startup>();
使用Microsoft.Extensions.Logging;
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(日志=>
{
logging.AddAzureWebAppDiagnostics();
})
.UseStartup();
现在您可以注入并使用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");
公共启动(IConfiguration配置,ILogger记录器)
{
配置=配置;
this.logger=记录器;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
logger.LogWarning(“启动”);
然后在Azure应用程序服务中,单击日志流:
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");