user2530833
user2530833

Reputation: 1017

C# Azure function ILoggerFactory Not logging with reference library

I created a simple Azure Function using c# and I would like to implement ILoggerFactory for the other class library. Here is my code.

MyFunction => Class1 => Class2

namespace FunctionApp1
{
    public class MyFunction
    {
        private readonly ILogger _log;
        private readonly IClass1 _class1;

        public MyFunction(ILoggerFactory loggerFactory, IClass1 class1)
        {
            _log = loggerFactory.CreateLogger<MyFunction>();
            _class1 = class1;
        }

        [FunctionName("Function1")]
        public IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
        {
            _log.LogCritical("LogCritical MyFunction");
            _log.LogInformation("LogInformation MyFunction");
            _log.LogError("LogError MyFunction");
            _log.LogWarning("LogWarning MyFunction");

            string value = _class1.Test1(23);

            return new OkObjectResult($"Hello, {value}");
        }
    }
}

namespace ClassLibrary1
{
    public interface IClass1
    {
        string Test1(int x);
    }

    public class Class1 : IClass1
    {
        private readonly ILogger _log;
        private readonly IClass2 _class2;

        public Class1(ILoggerFactory loggerFactory, IClass2 class2)
        {
            _log = loggerFactory.CreateLogger<Class1>();
            _class2 = class2;
        }

        public string Test1(int x)
        {
            _log.LogCritical("LogCritical Class1");
            _log.LogInformation("LogInformation Class1");
            _log.LogError("LogError Class1");
            _log.LogWarning("LogWarning Class1");

            return _class2.Test2(x);
        }
    }
}

namespace ClassLibrary2
{
    public interface IClass2
    {
        string Test2(int x);
    }
    public class Class2 : IClass2
    {
        private readonly ILogger _log;

        public Class2(ILoggerFactory loggerFactory)
        {
            _log = loggerFactory.CreateLogger<Class2>();
        }

        public string Test2(int x)
        {
            _log.LogCritical("LogCritical Class2");
            _log.LogInformation("LogInformation Class2");
            _log.LogError("LogError Class2");
            _log.LogWarning("LogWarning Class2");

            return $"Entered : {x}";
        }
    }
}

[assembly: WebJobsStartup(typeof(FunctionApp1.StartUp))]
namespace FunctionApp1
{
    public class StartUp : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddSingleton<IClass1, Class1>();
            builder.Services.AddSingleton<IClass2, Class2>();
        }
    }
}

My Problem is at run time it only logs the one in the FunctionApp1 class. Is there a way to log the info or error in other class rather than the main function app?

enter image description here

Upvotes: 5

Views: 1128

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

Update:

how to find host.json in azure portal:

1. enter image description here

2. enter image description here


Please try to add log level with the respective namespace_name.class_Name into host.json:

Here is the host.json file(make sure right click the host.json -> Properties -> set "Copy to Output Directory" as "Copy if newer"):

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "ClassLibrary1.Class1": "Information",
      "ClassLibrary2.Class2": "Information"
    }
  }
}

Then, all the logs are printed out:

enter image description here

For more details, you can refer to this GitHub issue.

Upvotes: 5

Related Questions