我目前正在使用Azure Functions,并且刚刚找到此接口定义。Assembly Microsoft.Extensions.Logging.Abstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60using System;namespace Microsoft.Extensions.Logging{ public interface ILogger { IDisposable BeginScope<TState>(TState state); bool IsEnabled(LogLevel logLevel); void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter); }}我特别感兴趣void Log<TState>。这个函数看起来像一个泛型,但似乎神奇地扩展为6个函数。log.LogCritical("...");log.LogDebug("...");log.LogError("...");log.LogInformation("...");log.LogTrace("...");log.LogWarning("...");我通过Azure Function定义收到对日志的引用。[FunctionName("WhoAmI")]public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, ILogger log){ ... }Microsoft样本和文档反映了这一点。我猜这是C#或Visual Studio功能,而不是巫术,但是它是哪个功能?
1 回答
慕桂英4014372
TA贡献1871条经验 获得超13个赞
这是称为扩展方法的C#功能。这些方法在LoggerExtensions静态类中定义。这是一个示例签名:
public static void LogDebug (
this ILogger logger, EventId eventId, string message, object[] args);
它是this使呼叫看起来像是接口成员的关键字。
- 1 回答
- 0 关注
- 165 浏览
添加回答
举报
0/150
提交
取消