'How to write to the Azure Log Stream
I am trying to get achieve something similar to Console.WriteLine()
in Azure Portal. From what I can find, you need to use System.Diagnostics.Trace
for that. But I cannot get anything to show.
Here is what I have done:
I have configured the App Service logs to write to the filesystem like this:
And then I have made a simple end point, which tests three different ways of writing to the log:
[HttpGet]
public IActionResult WriteToLog()
{
System.Diagnostics.Trace.TraceInformation("Getting all users... (TraceInformation)");
System.Diagnostics.Trace.WriteLine("Getting all users... (Trace.WriteLine)");
Console.WriteLine("Getting all users (Console.WriteLine)");
return Ok();
}
But nothing show in the Log Stream when I am calling the end point. Am I missing something? I found this post where one of the answers mentions configuring AzureFileLoggerOptions, but I can't really find anything concrete about how or why you need this?
Solution 1:[1]
- By default, ASP.NET Core uses the
Microsoft.Extensions.Logging.AzureAppServices
logging provider. System.Diagnostics.Trace
doesn't use configuration that .NET Core is built upon.- If you really want to use
System.Diagnostics.Trace
, you'll have to add as a dependency yourself to your app. - ILogger, which can write messages to Application Logs, can be used as a workaround for.NET core web applications.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//your other code
//add the following 2 lines of code.
loggerFactory.AddConsole();
loggerFactory.AddDebug();
app.UseStaticFiles();
//your other code
}
Please refer Logging in .NET Core for more information
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | HarshithaVeeramalla-MT |