英文:
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("0 0 10 * * *")] Ilogger logger)
{
await Method2()
}
public static async Task<String> Method2()
{
Method3()
//Here I need to also log using Ilogger.
}
public static async Task<String> 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<Function1> logger;
public Function1(ILogger<Function1> logger)
{
Function1.logger = logger;
}
public async Task Run([TimerTrigger("0 0 10 * * *")])
{
await Method2()
logger.Loginformation("Log stuff");
}
public static async Task<String> Method2()
{
//Here I need to also log using Ilogger.
logger.Loginformation("Log stuff");
}
...
}
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<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()
{
this.logger.Loginformation("Log stuff");
}
...
}
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<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()
{
//Here I need to also log using Ilogger.
this.logger.Loginformation("Log stuff");
}
...
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论