简体   繁体   English

从作为 Azure Web 应用程序托管的 ASP.NET 5 应用程序进行日志记录

[英]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.

相关问题 Asp.net Core Azure Web应用程序日志记录 - Asp.net Core azure web app logging 如何从托管在 Azure 应用服务中的 .Net Framework Asp.Net 应用程序访问 Azure KeyValut? - How do I access an Azure KeyValut from an .Net Framework Asp.Net application hosted in an Azure App Service? 当托管在Azure应用程序服务中时,如何控制添加到ASP.NET核心Web应用程序中的HTTP响应标头? - How can I control the HTTP response headers added to my ASP.NET core web application when hosted in Azure app service? 无法连接托管在 Azure Web 应用程序上的安全 Asp.Net Core Web 套接字(使用 TLS) - Can't connect secured Asp.Net Core Web Socket hosted on Azure Web App (using TLS) Azure“ Web应用程序”中的ASP.Net应用程序缓存 - ASP.Net Application cache in an Azure “Web App” 从 Azure 中托管的 Asp.net Core MVC Web App 获取“响应状态代码并不表示成功:502(坏网关)” - Getting "Response status code does not indicate success: 502 (Bad Gateway)" from Asp.net Core MVC Web App hosted in Azure 使用Azure上托管的ASP.NET Core Web应用程序将文件上传到目录 - Uploading files to a directory with an ASP.NET Core Web Application hosted on Azure 有没有办法在 Azure 上使用托管 ASP.NET 核心 web 应用程序的本地数据库? - Is there a way to use a local db with a hosted ASP.NET core web app on Azure? 将客户端 IP 从 ASP.NET Web API 转发到 Azure 中托管的 .NET Core API - Forward client IP from ASP.NET Web API to .NET Core API hosted in Azure ASP.NET Web应用程序Azure设置 - ASP.NET Web Application Azure Settings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM