从作为Azure Web App托管的ASP.NET 5应用程序进行日志记录 [英] Logging from ASP.NET 5 application hosted as Azure Web App
问题描述
我有一个ASP.NET 5 Web API,我将其作为Web App托管在Azure中.我想使用Azure Diagnostics从我的代码中记录消息.有多篇文章,包括
但是以下调用不会产生日志:
System.Diagnostics.Trace.TraceError("TEST");System.Diagnostics.Trace.TraceInformation("TEST");System.Diagnostics.Trace.TraceWarning("TEST");System.Diagnostics.Trace.WriteLine("TEST");
我试图手动定义 TRACE
符号,但是没有运气:
我还尝试使用新的 Microsoft.Extensions.Logging
框架并使用 ILogger.Log
API,但再次没有任何结果:
public void Configure(IApplicationBuilder应用,IHostingEnvironment env,ILoggerFactory loggerFactory){loggerFactory.MinimumLevel = LogLevel.Debug;var sourceSwitch = new SourceSwitch("Sandbox.AspNet5.ApiApp-Demo");sourceSwitch.Level = SourceLevels.All;loggerFactory.AddTraceSource(sourceSwitch,新的ConsoleTraceListener(false));loggerFactory.AddTraceSource(sourceSwitch,新的EventLogTraceListener("Application"));}
关于我在做什么错的任何想法?
我为azure应用找到了一个简单的技巧,请参阅 或用于自定义设置: 并在Azure上启用诊断日志,同时在Blob和File上都可以正常工作.不需要任何额外的配置.:) I have an ASP.NET 5 Web API that I host in Azure as a Web App. I want to log messages from my code using Azure Diagnostics. There are multiple article including Azure docs that suggest that it should be as easy as I enabled application logging for the web app: But the following calls produces no logs: I tried to manually define I also tried to use the new Any ideas on what am I doing wrong? I've found an simple trick for azure app ,see https://github.com/aspnet/Logging/tree/dev/src/Microsoft.Extensions.Logging.AzureAppServices,
please add the package "Microsoft.Extensions.Logging.AzureAppServices": "1.0.0-preview1-final" and update related dependencies,
add the azure diagnostics in startup.cs like this : or for custom setting: And enable the diagnostics log on azure, both logging on blob and file are work well. No need for any
extra configuration. :) 这篇关于从作为Azure Web App托管的ASP.NET 5应用程序进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
loggerFactory.AddConsole(Configuration.GetSection("Logging"));loggerFactory.AddDebug();loggerFactory.AddAzureWebAppDiagnostics();//为默认设置.
loggerFactory.AddAzureWebAppDiagnostics(new AzureAppServicesDiagnosticsSettings(...));//添加自定义设置.//有关详细的成员属性,请参见此处:https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.AzureAppServices/AzureAppServicesDiagnosticsSettings.cs
System.Diagnostics.Trace.WriteLine
once enabled. The logs should show up under LogsFiles/Application
and in log stream in Azure.System.Diagnostics.Trace.TraceError("TEST");
System.Diagnostics.Trace.TraceInformation("TEST");
System.Diagnostics.Trace.TraceWarning("TEST");
System.Diagnostics.Trace.WriteLine("TEST");
TRACE
symbol, but with no luck:Microsoft.Extensions.Logging
framework and use ILogger.Log
API, but without any results again:public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Debug;
var sourceSwitch = new SourceSwitch("Sandbox.AspNet5.ApiApp-Demo");
sourceSwitch.Level = SourceLevels.All;
loggerFactory.AddTraceSource(sourceSwitch,
new ConsoleTraceListener(false));
loggerFactory.AddTraceSource(sourceSwitch,
new EventLogTraceListener("Application"));
}
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddAzureWebAppDiagnostics(); // for default setting.
loggerFactory.AddAzureWebAppDiagnostics(new AzureAppServicesDiagnosticsSettings( ...)); // add custom setting.
// see here for detailed member properties: https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.AzureAppServices/AzureAppServicesDiagnosticsSettings.cs