1

I am trying to use another logger than the standard Tracewriter in Azure function. Mostly NLog which all works locally when running the Azure Function but nothing gets written after I deployed to Azure.

Load config through blob. I store my config file in the storage blob and loads it in as a parameter

[FunctionName("Update")]
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "put", Route = null)]HttpRequest req, 
        [Blob("nlog/nlog.config", FileAccess.Read)] string inputBlob)

Logger.LoadConfiguration(inputBlob);
Logger.Info("This does not show in Azure");

...

My logger class where i load the configuration

public class Logger
    {
        public static Logger Log = LogManager.GetCurrentClassLogger();

        private static bool _isConfigrued;

        public static void LoadConfiguration(string xmlString)
        {
            if (string.IsNullOrEmpty(xmlString))
                return;

            if (!_isConfigrued)
            {
                StringReader sr = new StringReader(xmlString);
                XmlReader xr = XmlReader.Create(sr);
                LogManager.Configuration = new XmlLoggingConfiguration(xr, null);

                Log = LogManager.GetCurrentClassLogger();
                _isConfigrued = true;
            }
        }
    }

I then configure the the NLog to write to trace

  <targets>
    <target name='console' type='Console' layout='Console ${message}' />
    <target name="trace" xsi:type="Trace" layout="Trace ${message}" />
  </targets>

 <rules>
    <logger name="*" minlevel="Trace" writeTo="trace" />
    <logger name="*" minlevel="Trace" writeTo="console" /> 
  </rules>

But nothing gets written to trace/console

I also tried loading the config like suggested here

https://blogs.msdn.microsoft.com/waws/2017/03/16/nlog-and-database/

No luck either

I also tried the Microsoft.Extentions.Loggin.Ilogger, works locally but nothing gets printed on Azure.

No exceptions are thrown just and empty console window.

Julian
  • 33,915
  • 22
  • 119
  • 174
Jepzen
  • 2,942
  • 6
  • 40
  • 62
  • If you are configuring it in `app.config`, this won't work since Functions don't use that file. – Mikhail Shilkov Aug 01 '18 at 11:23
  • I load the config file from the blob storage or from the file system and in the link – Jepzen Aug 01 '18 at 11:24
  • Where are you checking for your logs exactly? In the Functions portal, the logs you see there are logs streaming from the file system, not from console logs. – brettsam Aug 01 '18 at 13:47
  • I read somewhere here SO that the target xsi:type"Trace" would go to the streaming. Just like the original tracewriter – Jepzen Aug 01 '18 at 14:17
  • I can confirm, that for v2 using `TraceWriter` does not outputs anything to the console in Azure Portal. – kamil-mrzyglod Aug 02 '18 at 08:48

1 Answers1

0

When running on Azure ver. 1 then one can forward NLog-output to a TextWriter logger:

When running on Azure ver. 2 then one can forward NLog-output to a Microsoft ILogger:

See also: https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-cloud-logging-with-Azure-function-or-AWS-lambda

See also: https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library

If needing to deploy nlog.config, then one might have to include it in the "Application Settings" for the Azure function. See also https://stackoverflow.com/a/47332193

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70