代码之家  ›  专栏  ›  技术社区  ›  Sivabalakrishnan

如何在Azure functionpp中将Microsoft.Extensions.Logging.ILogger用作类变量?

  •  1
  • Sivabalakrishnan  · 技术社区  · 6 年前

    问题:

    如何将ILogger声明为类变量并将其用于日志记录?

    1. 我需要开发以tracewriter为默认值的Azure函数应用程序。 编写UnitTest时,tracewriter很困难。取而代之的是我使用了ILogger。

    2. 在函数应用程序中,iam将日志变量从初始运行方法传递给所有方法。(这不是正确的方法。)

    3. 我需要将ILogger声明为一个类变量,并在所有地方使用它们。

    所以,怎么做。

    示例代码

    public class addClass
        {
          //  private static ILogger LOGGER = ---???-----  // I need to declare here.
    
            private void add(ILogger log)       // Iam using like this. This is not rite way of programming to pass to all methods.
            {
              int a = 10;
              int b=4;
    
                    log.LogInformation((a+b));
    
            }
    
            private void addM()     // Using the class variable.    
            {
              int a = 10;
              int b = 4;
    
                    log.LogInformation((a+b));
    
            }
        }
    

    0 回复  |  直到 6 年前