Azure app service diagnostic blob not logging nlog based application logs

Ram Shankar 1 Reputation point
2021-01-18T20:23:14.987+00:00

I 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()  
        .ConfigureWebHostDefaults(webBuilder =>  
        {  
            webBuilder.UseStartup<Startup>();  
        });  
    }  
}  

The Nlog based application logs are not logged in app service diagnostic blob, instead only default logging is printed.
Kindly help to resolve this issue.
57977-blob-log.jpg

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,529 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,931 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2021-01-19T21:07:21.147+00:00

    Hi @Ram Shankar ,

    Please have a look at https://github.com/JDetmar/NLog.Extensions.AzureStorage. You'll need to add the nuget package and configure NLog to use Azure Blob Storage by setting it as a target in the web.config file. The snippet below was taken from the README.md.

        <target type="AzureBlobStorage"  
                name="Azure"  
                layout="${longdate:universalTime=true} ${level:uppercase=true} - ${logger}: ${message} ${exception:format=tostring}"  
                connectionString="DefaultEndpointsProtocol=https;AccountName=##accountName##;AccountKey=##accountKey##;EndpointSuffix=core.windows.net"  
                container="${machinename}"  
                blobName="${logger}/${date:universalTime=true:format=yy-MM-dd}/${date:universalTime=true:format=HH}.log">  
                    <metadata name="mymeta" layout="mymetavalue" />   <!-- Multiple allowed -->  
                    <tag name="mytag" layout="mytagvalue" /> <!-- Multiple allowed -->  
    

    By adding the additional target, NLog should send the logging information to your blob storage.

    Regards,
    Ryan

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.