5

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

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
user2530833
  • 1,008
  • 1
  • 10
  • 25
  • I found the answer here. https://stackoverflow.com/questions/55408032/configuring-log-level-for-azure-functions – user2530833 Jan 15 '20 at 02:48

1 Answers1

5

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.

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • Hmmm, it seems working. But, I have a lot of Class Libraries and Classes within my application. Should I put everything in the host.json? And if I deploy it in my azure portal where can I find the configuration for this? Thanks! – user2530833 Jan 13 '20 at 03:51
  • @user2530833, if you still have questions, please let me know. And if the post is helpful, could you please help accept it as answer? Thanks. – Ivan Glasenberg Jan 14 '20 at 07:25
  • @user2530833, if the namespace is not started with "Function", you should put everything in the host.json file; if the namespace is started with "Function", like "FunctionClassLibrary1", then do not need to put it in host.json . And after deploying to azure, you can find the host.json in "Platform features" -> General Settings -> Function app settings. I attached an image of how to find host.json in my post -> updated section. – Ivan Glasenberg Jan 14 '20 at 07:34