如何在不同方法中使用Ilogger?

huangapple go评论54阅读模式
英文:

How to use Ilogger in different methods?

问题

我想要能够在我的Azure Function中使用ILogger记录不同方法中的不同事务。标准做法是在方法参数中使用ILogger。但对我来说行不通,因为我需要能够在不带参数的情况下调用我的方法。

示例:

```csharp
public async Task Run([TimerTrigger("0 0 10 * * *")] ILogger logger)
{
    await Method2();
}

public async Task<string> Method2()
{
    Method3();
    // 这里我也需要使用ILogger记录。
}

public async Task<string> Method3()
{
    // 这里我也需要使用ILogger记录。
}

我认为解决方案应该是将ILogger logger也用作Method2的参数。但这会导致一个问题,因为我还需要在Run方法中使用它。

所以我不知道如何在不同的方法中记录日志。

总共我有5个需要记录日志的方法。

我已经对ILogger进行了一些研究,但我找不到与我的直接问题相关的内容。

我认为可以尝试在类中添加ILogger的构造函数,如下所示:

public class Function1
{
    private ILogger<Function1> logger;

    public Function1(ILogger<Function1> logger)
    {
        this.logger = logger;
    }

    public async Task Run([TimerTrigger("0 0 10 * * *")])
    {
        await Method2();
        logger.LogInformation("记录内容");
    }

    public async Task<string> Method2()
    {
        // 这里我也需要使用ILogger记录。
        logger.LogInformation("记录内容");
    }
    ...
}

我阅读过这个可能有效。我已经进行了一些调试,并做了一些更改,但没有成功。

如果有人对如何解决这个问题有任何想法或解决方案,请告诉我。

编辑:

我已经将其更改为非静态,并改成了以下方式:

public class Function1
{
    private ILogger<Function1> logger;

    public Function1(ILogger<Function1> logger)
    {
        this.logger = logger;
    }

    public async Task Run([TimerTrigger("0 0 10 * * *")])
    {
        await Method2();
        this.logger.LogInformation("记录内容");
    }

    public async Task<string> Method2()
    {
        this.logger.LogInformation("记录内容");
    }
    ...
}

我没有异常,我尝试过调试,但不幸的是没有找到问题的原因。


<details>
<summary>英文:</summary>

I want to be able to use Ilogger to log different things in different methods for my Azure Function. As a standard the Ilogger is used in the methods parameters. But this wont work for me as I need to be able to call my methods without parameters. 

Example:

    public async Task Run([TimerTrigger(&quot;0 0 10 * * *&quot;)] Ilogger logger)
    {
        await Method2()
    }

    public static async Task&lt;String&gt; Method2()
    {
      Method3()
      //Here I need to also log using Ilogger.
    }

    public static async Task&lt;String&gt; Method3()
    {
      //Here I need to also log using Ilogger.
    }
The solution would be in my head to also have the Ilogger logger used as a parameter for the Method2. But this will lead to a problem, as I then need to have it in my Run method.

So I do not know how I am able to log in different methods. 

I have in total 5 methods that it would be needed to log in.

I have done some research on the Ilogger, but I could not find anything relating to my direct problem. 

I thought making a constructor with the Ilogger in like so:

public class Function1
{

    private static ILogger&lt;Function1&gt; logger;

    public Function1(ILogger&lt;Function1&gt; logger)
    {
        Function1.logger = logger;
    }

    public async Task Run([TimerTrigger(&quot;0 0 10 * * *&quot;)])
    {
        await Method2()
        logger.Loginformation(&quot;Log stuff&quot;);
    }

   public static async Task&lt;String&gt; Method2()
   {
     //Here I need to also log using Ilogger.
     logger.Loginformation(&quot;Log stuff&quot;);
   }

...
}

I have read this could work. I have done some debugging with this and changed a bit, but without luck.

If anyone have some ideas or solution on how to solve this, please do let me know :)



**EDIT:** 

I have made it so it is no longer static, and changed it to the following:

public class Function1
{

    private ILogger&lt;Function1&gt; logger;

    public Function1(ILogger&lt;Function1&gt; logger)
    {
        this.logger = logger;
    }

    public async Task Run([TimerTrigger(&quot;0 0 10 * * *&quot;)])
    {
        await Method2()
        this.logger.Loginformation(&quot;Log stuff&quot;);
    }

   public async Task&lt;String&gt; Method2()
   {
     this.logger.Loginformation(&quot;Log stuff&quot;);
   }

...
}


I have no exceptions, I tried to debug this, but unfortunately without luck to find the reason for the problem.

</details>


# 答案1
**得分**: 2

不要为记录器使用静态属性:

``` CSharp
public class Function1
{
    private readonly ILogger<Function1> logger;

    public Function1(ILogger<Function1> logger)
    {
        this.logger = logger;
    }

    public async Task Run([TimerTrigger("0 0 10 * * *")]
    {
        await Method2();
        this.logger.LogInformation("Log stuff");
    }

    public async Task<string> Method2()
    {
        // 这里我也需要使用ILogger记录。
        this.logger.LogInformation("Log stuff");
    }
    ...
}

使用非静态属性,您可以从类的任何位置访问记录器,因此不需要将其作为方法参数添加。

将您的方法设为非静态将允许您使用this来访问与实例相关的属性。

英文:

Do not use a static property for the logger :

 public class Function1
    {

        private readonly ILogger&lt;Function1&gt; logger;

        public Function1(ILogger&lt;Function1&gt; logger)
        {
            this.logger = logger;
        }

        public async Task Run([TimerTrigger(&quot;0 0 10 * * *&quot;)])
        {
            await Method2()
            this.logger.Loginformation(&quot;Log stuff&quot;);
        }

       public async Task&lt;String&gt; Method2()
       {
         //Here I need to also log using Ilogger.
         this.logger.Loginformation(&quot;Log stuff&quot;);
       }
   ...
   }

Using a non-static property, you will be able to access your logger from everywhere in your class. So no need to add it as a method parameter.

Making your method non-static will allow you to use this to access your instance related properties.

答案2

得分: 0

不要使用静态属性来记录器!

  • 您可以将记录器实例作为参数传递给静态方法。
  • 或者将静态方法更改为非静态,并通过非静态属性访问记录器。
英文:

You definitely should not use a static property for the logger!

  • You can pass the logger instance as parameter to the static methods.

  • Or you make the static methods non-static and access the logger via a non static property.

huangapple
  • 本文由 发表于 2023年4月11日 17:04:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定