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?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Update:
how to find host.json in azure portal:
1.
2.
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:
For more details, you can refer to this GitHub issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With