1
votes

enter image description hereI want to log nlog generated application logs in app service diagnostic blob [i.e, Application Logging (Blob) ] but only default logs are printed not the nlog based custom logs but I can print Application Logging (Filesystem) when file target is added to nlog.config. The problem is only with blob.

nlog.config file:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwConfigExceptions="true"
      internalLogLevel="info"
      internalLogFile="d:\home\LogFiles\temp\internal-nlog-AspNetCore3.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <target xsi:type="Trace" name="String" layout="${level}\: ${logger}[0]${newline} |trace|     ${message}${exception:format=tostring}" />
    <target xsi:type="Console" name="lifetimeConsole" layout="${level}\: ${logger}[0]${newline} |console|     ${message}${exception:format=tostring}" />
  </targets>

 <rules>
      <logger name="*" minlevel="Trace" writeTo="lifetimeConsole,String" final="true"/>
    </rules>
</nlog>

program.cs file

namespace testapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
            try
            {
                logger.Debug("init main");
                CreateHostBuilder(args).Build().Run();
            }
            catch (Exception exception)
            {
                logger.Error(exception, "Stopped program because of exception");
                throw;
            }
            finally
            {
                NLog.LogManager.Shutdown();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logging =>
            {
                logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                logging.AddConsole();
                logging.AddDebug();
                logging.AddAzureWebAppDiagnostics();
            })
              .UseNLog() // NLog: Setup NLog for Dependency injection
            .ConfigureServices(serviceCollection => serviceCollection
                    .Configure<AzureBlobLoggerOptions>(options =>
                    {
                        options.BlobName = "testlog.txt";
                    }))
              .ConfigureWebHostDefaults(webBuilder =>
              {
                  webBuilder.UseStartup<Startup>();
              }); 
    }

}

The Nlog based loggings are not logged in app service diagnostic blob, instead only default logging is printed. Kindly help to resolve this issue.

1
Maybe you forgot to configure AzureFileLoggerOptions. See also docs.microsoft.com/en-us/dotnet/core/extensions/…Rolf Kristensen
Also check that you have not enabled WEBSITE_LOCAL_CACHE_OPTIONRolf Kristensen
Could also be that NLog.config has not been deployed properly. See also github.com/NLog/NLog/wiki/Logging-troubleshootingRolf Kristensen
@RolfKristensen The issue is I can print nlog logs in Application Logging (Filesystem) when using nlog file target. The issue is only with Application Logging (Blob) which is having only default loggings instead of nlog based logs.ram shankar
@RolfKristensen I don't know which nlog target is accepted in Application Logging (Blob), I'm trying with console and trace.ram shankar

1 Answers

0
votes

Seems that System.Diagnostics.Trace-Target works best for ASP.NET-application and not for ASP.NetCore applications in Azure.

When using AddAzureWebAppDiagnostics it will redirect all output written to Microsoft ILogger to FileSystem or Blob. But any output written to pure NLog Logger-objects will not be redirected.

Maybe the solution is to setup a NLog FileTarget writing to the HOME-directory:

    <nlog>
         <targets async="true">
             <!-- Environment Variable %HOME% matches D:/Home -->
             <target type="file" name="appfile" filename="${environment:HOME:cached=true}/logfiles/application/app-${shortdate}-${processid}.txt" />
         </targets>
         <rules>
             <logger name="*" minLevel="Debug" writeTo="appFile" />
         </rules>
    </nlog>

See also: https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#stream-logs

For including Blob-output then take a look at NLog.Extensions.AzureBlobStorage

Alternative to Blob-output could be ApplicationInsights but it might have different pricing.