Logging from ASP.NET 5 application hosted as Azure Web App
Asked Answered
S

3

21

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 System.Diagnostics.Trace.WriteLine once enabled. The logs should show up under LogsFiles/Application and in log stream in Azure.

I enabled application logging for the web app:

enter image description here

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:

enter image description here

I also tried to use the new 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"));
}

Any ideas on what am I doing wrong?

Silk answered 19/1, 2016 at 0:41 Comment(5)
Hello, have you checked your web.config? is there a clear in the listeners or have you registered the azure one correctly? azure.microsoft.com/en-us/documentation/articles/…Admire
Start the log stream from Visual Studio, from the Cloud Explorer window (go to the website, right click, view streaming logs). This will enable log streaming for an hour. The Log stream on the KUDU portal doesn't always start is my experience.Triarchy
@MikeMiller Are the instructions you are referring to the same for asp.net5?Scintillometer
@ErikOppedijk How is that related to the question?Scintillometer
@MagnusKarlsson this is regarding the very last part of his question: "The logs should show up under LogsFiles/Application and in log stream in Azure."Triarchy
O
9

If you look at your web.config, it probably has stdoutLogEnabled="false". If you set that to true, then anything that is written to standard output will be written to a file. And the stdoutLogFile determines where it goes, which by default is under d:\home\logfiles.

Now you need to make sure that your logging actually goes to stdout. Doing Console.WriteLine would definitely work. I think it's probably possible to also configure things via ILoggerFactory in your startup.cs to make logs go to stdout.

Orfurd answered 27/1, 2016 at 0:2 Comment(6)
This will write the logs to the web applications file system and I was hoping to find out how to get them to azure blob storage the same way it worked with "old" asp.net. You are getting the bounty by grace if no one provdes a working solution to get the logs to Azure blob storage.Scintillometer
There is no support for that yet, though it will come. You could conceivably write your own support (e.g. based on github.com/davidebbo-test/ConsoleInterceptor), but that would involve a bit of work.Orfurd
Ok, Do you know if it will take a month or three(or six) for it to be ready?Scintillometer
Sorry, no clear ETA, but 2 or 3 months is probably the ballpark.Orfurd
But ASP.NET 5 (ASP.NET Core) apps don't have a web.config do they?Rancourt
@IanGriffiths there actually is a web.config when running on IIS.Orfurd
P
9

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 :

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. No need for any extra configuration. :)

Pryor answered 4/11, 2016 at 2:42 Comment(0)
R
4

I got Streaming Log output working for an ASP.NET 5 web app by writing the following class (based on David Ebbo's example):

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:

Trace.Listeners.Add(new AzureApplicationLogTraceListener());

This only supports filesystem-based logging (which is sufficient for live log streams).

Rancourt answered 3/2, 2016 at 8:0 Comment(2)
Does this just work for System.Diagnostics.Trace.*, or does it also work for using ILogger?Adamite
Worked like a charm. Thanks.Threegaited

© 2022 - 2024 — McMap. All rights reserved.