Reputation: 17562
I'm currently working with Azure Functions and I just found this interface definition.
Assembly Microsoft.Extensions.Logging.Abstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
using 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);
}
}
I am particular interested in void Log<TState>
. This function has what looks like a generic, but seems to magically expand into 6 functions.
log.LogCritical("...");
log.LogDebug("...");
log.LogError("...");
log.LogInformation("...");
log.LogTrace("...");
log.LogWarning("...");
I receive the reference to log via the Azure Function defintion.
[FunctionName("WhoAmI")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, ILogger log)
{ ... }
The Microsoft samples and documentation reflect this.
I'm guessing this is a C# or Visual Studio feature and not witchcraft, but which feature is it?
Upvotes: 0
Views: 110
Reputation: 35134
This is C# feature called Extension Methods. These methods are defined in LoggerExtensions
static class. Here is a sample signature:
public static void LogDebug (
this ILogger logger, EventId eventId, string message, object[] args);
It's the keyword this
which makes the call look like it's the member of interface.
Credits to @SirRufo for giving the right hint in the comments. I just don't want to leave this question without the full answer :)
Upvotes: 4