2

I have a simple Azure Function and I'd like to be able to monitor log output in both the streaming log window and in Application Insights.

So far I am able to see NLog output in Application Insights but not in the streaming window. Microsoft ILogger output appears in both.

Here's what I've done so far:

  • Created a basic Azure test function
  • From the Azure portal, I enabled Application Insights
  • Installed NLog and Microsoft.ApplicationInsights.NLogTarget nuget dependencies for the function.
  • Added code to the function to programmatically create an Application Insights nlog target.
  • After reading these questions... Azure Log Streaming with NLog and How to integrate NLog to write log to Azure Streaming log I have also programmatically added an NLog TraceTarget since they suggest that the streaming log mechanism only displayed the Trace ouput stream (not to be confused with the log-level called 'Trace' !)
  • Finally, I have modified host.json to turn the default logging level up to "Trace". This is required in order for the streaming window to show Trace-level output for the Microsoft ILogger and I had thought it might be the reason none of the Nlog output was showing...

    {
     "version": "2.0",
     "logging": {
       "logLevel": {
         "default": "Trace"
        }
      }
    }
    

So far, the results are that:

  • All Microsoft logger output is shown in both the streaming window and Application Insights log.
  • The Nlog output is shown in Application Insights but does NOT appear in the streaming window.

Here is the final function code...

public static class Function1
{
    [FunctionName("LogTest")]
    public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
    {
        // Set up NLOG targets and logger
        var config = new LoggingConfiguration();
        //send logging to application insights     
        config.LoggingRules.Add(
          new LoggingRule("*", LogLevel.Trace, 
          new ApplicationInsightsTarget()));
        //also try to log to Trace output 
        //'RawWrite' is used to try and force output at all log-levels  
        config.LoggingRules.Add(
          new LoggingRule("*", LogLevel.Trace, 
          new TraceTarget {RawWrite = true}));

        LogManager.Configuration = config;
        var nlog = LogManager.GetLogger("Example");

        //log using native
        log.LogInformation($"log:Info"); //appears in live-stream, app-insights
        log.LogError("log:Error");       //appears in live-stream, app-insights
        log.LogTrace("log:Trace");       //appears in live-stream, app-insights (after modifying host.json)

        //log using nlog
        nlog.Info("nlog:info");          //appears in ...........  app-insights
        nlog.Error("nlog:error");        //appears in ...........  app-insights
        nlog.Trace("nlog:trace");        //appears in ...........  app-insights                     

        //say goodbye
        log.LogInformation("log:ending");
    }
}

Thanks in advance for any suggestions - no doubt I'm missing some simple step.

Julian
  • 33,915
  • 22
  • 119
  • 174
NeilMacMullen
  • 3,339
  • 2
  • 20
  • 22
  • https://stackoverflow.com/questions/42439564/azure-log-streaming-with-nlog – Carlos Alves Jorge May 22 '19 at 13:58
  • Thanks Carlos but as I already mentioned in my post, I have read that Q&A and the related one. Both of those refer to using a config file whereas I am trying to do this with the (equivalent) programmatic interface. – NeilMacMullen May 22 '19 at 14:28
  • 2
    Maybe this can help: https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-cloud-logging-with-Azure-function-or-AWS-lambda ? – Rolf Kristensen May 22 '19 at 15:20
  • 1
    Please check https://github.com/nlog/nlog/wiki/Logging-troubleshooting – Julian May 22 '19 at 15:26
  • I'll check those links in more detail later but just to reiterate... NLog _is logging correctly on the ApplicationInsights target with exactly the same rules as the TraceTarget. The issue is that the TraceTarget output is not making its way to the Streamed Log console in the Azure Portal. There is an interesting looking section in that first link which appears to show how to connect NLog up to the ILogger instance so I'll give that a go later. – NeilMacMullen May 22 '19 at 15:36

1 Answers1

4

Thanks to the link provided by Rolf Kristensen
it seems the solution is to configure a new rule using the MicrosoftILoggerTarget rather than the TraceTarget.

So the complete code is

var config = new LoggingConfiguration();
//ensure that log output is seen in the Streamed Log window
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, 
     new MicrosoftILoggerTarget(azureLog)));
//ensure that log output is sent to Application Insights
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, 
     new ApplicationInsightsTarget()));
LogManager.Configuration = config;
var nlog = LogManager.GetLogger("Example");
nlog.Info("output from nlog");

where azureLog is the ILogger provided to the Run method of the function.

MicrosoftILoggerTarget can be found in the NLog.Extensions.Logging nuget package.

Julian
  • 33,915
  • 22
  • 119
  • 174
NeilMacMullen
  • 3,339
  • 2
  • 20
  • 22