[英]Logging from ASP.NET 5 application hosted as Azure Web App
I have an ASP.NET 5 Web API that I host in Azure as a Web App.我有一个 ASP.NET 5 Web API,作为 Web 应用程序托管在 Azure 中。 I want to log messages from my code using Azure Diagnostics.
我想使用 Azure 诊断从我的代码中记录消息。 There are multiple article including Azure docs that suggest that it should be as easy as
System.Diagnostics.Trace.WriteLine
once enabled.包括Azure 文档在内的多篇文章表明,一旦启用它就应该像
System.Diagnostics.Trace.WriteLine
一样简单。 The logs should show up under LogsFiles/Application
and in log stream in Azure.日志应显示在
LogsFiles/Application
和 Azure 中的日志流中。
I enabled application logging for the web app:我为 Web 应用程序启用了应用程序日志记录:
But the following calls produces no logs:但以下调用不产生日志:
System.Diagnostics.Trace.TraceError("TEST");
System.Diagnostics.Trace.TraceInformation("TEST");
System.Diagnostics.Trace.TraceWarning("TEST");
System.Diagnostics.Trace.WriteLine("TEST");
I tried to manually define TRACE
symbol, but with no luck:我试图手动定义
TRACE
符号,但没有运气:
I also tried to use the new Microsoft.Extensions.Logging
framework and use ILogger.Log
API, but without any results again:我还尝试使用新的
Microsoft.Extensions.Logging
框架并使用ILogger.Log
API,但再次没有任何结果:
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"));
}
Any ideas on what am I doing wrong?关于我做错了什么的任何想法?
If you look at your web.config
, it probably has stdoutLogEnabled="false"
.如果您查看您的
web.config
,它可能具有stdoutLogEnabled="false"
。 If you set that to true, then anything that is written to standard output will be written to a file.如果将其设置为 true,则写入标准输出的任何内容都将写入文件。 And the
stdoutLogFile
determines where it goes, which by default is under d:\\home\\logfiles
.并且
stdoutLogFile
确定它的stdoutLogFile
,默认情况下在d:\\home\\logfiles
。
Now you need to make sure that your logging actually goes to stdout.现在您需要确保您的日志记录实际上进入了标准输出。 Doing
Console.WriteLine
would definitely work.做
Console.WriteLine
肯定会工作。 I think it's probably possible to also configure things via ILoggerFactory
in your startup.cs
to make logs go to stdout.我认为也有可能通过
ILoggerFactory
在你的startup.cs
配置东西,使日志进入标准输出。
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 :我为 azure 应用程序找到了一个简单的技巧,请参阅https://github.com/aspnet/Logging/tree/dev/src/Microsoft.Extensions.Logging.AzureAppServices ,请添加包“Microsoft.Extensions.Logging.AzureAppServices ": "1.0.0-preview1-final" 并更新相关依赖,在startup.cs中添加azure诊断如下:
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddAzureWebAppDiagnostics(); // for default setting.
or for custom 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
And enable the diagnostics log on azure, both logging on blob and file are work well.并在 azure 上启用诊断日志,blob 和文件上的日志都运行良好。 No need for any extra configuration.
无需任何额外配置。 :)
:)
I got Streaming Log output working for an ASP.NET 5 web app by writing the following class (based on David Ebbo's example ):通过编写以下类(基于David Ebbo 的示例),我获得了适用于 ASP.NET 5 Web 应用程序的流日志输出:
public class AzureApplicationLogTraceListener : TraceListener
{
private readonly string _logPath;
private readonly object _lock = new object();
public AzureApplicationLogTraceListener()
{
string instanceId = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
if (instanceId != null)
{
string logFolder = Environment.ExpandEnvironmentVariables(@"%HOME%\LogFiles\application");
Directory.CreateDirectory(logFolder);
instanceId = instanceId.Substring(0, 6);
_logPath = Path.Combine(logFolder, $"logs_{instanceId}.txt");
}
}
public override void Write(string message)
{
if (_logPath != null)
{
lock (this)
{
File.AppendAllText(_logPath, message);
}
}
}
public override void WriteLine(string message)
{
Write(message + Environment.NewLine);
}
}
and then putting this in my Startup.Configure
:然后把它放在我的
Startup.Configure
:
Trace.Listeners.Add(new AzureApplicationLogTraceListener());
This only supports filesystem-based logging (which is sufficient for live log streams).这只支持基于文件系统的日志记录(对于实时日志流来说已经足够了)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.