When you are doing serverless development in Microsoft system, Azure Functions are one of the feature that you can’t miss.

As explained on the official website:

With Azure Function, you can build and debug locally without additional setup, deploy and operate at scale in the cloud and integrate services using triggers and bindings.

When you run an Azure Function, you’ll notice there is already a lot of useful information that are written in the output (for debug or information purposes):

Each function can also received an ILogger parameter so you can easily add your custom logging logic:

[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest httpRequest,
ILogger log)
{
}

Even if this method is working very well, it might be a bit complicated if you have tons of functions where you want to log, debug information, etc.
Fortunately, you can use IFunctionInvocationFilter and IFunctionExceptionFilter to raise your own logic when a function is running, when it has run or when an exception has occured:

public class LogFilter : IFunctionInvocationFilter, IFunctionExceptionFilter
{
    public Task OnExecutingAsync(FunctionExecutingContext executingContext, CancellationToken cancellationToken)
    {
        executingContext.Logger.LogInformation($"---- LOG: Executing function {executingContext.FunctionName} ----");

        return Task.CompletedTask;
    }

    public Task OnExecutedAsync(FunctionExecutedContext executedContext, CancellationToken cancellationToken)
    {
        executedContext.Logger.LogInformation($"---- LOG: Executed function {executedContext.FunctionName} ----");

        return Task.CompletedTask;
    }

    public Task OnExceptionAsync(FunctionExceptionContext exceptionContext, CancellationToken cancellationToken)
    {
        exceptionContext.Logger.LogInformation($"---- LOG: Exception in function {exceptionContext.FunctionName} ----");
        exceptionContext.Logger.LogInformation($"---- LOG: Exception : {exceptionContext.Exception.Message} ----");

        return Task.CompletedTask;
    }
}

Don’t forget to add your filter in the pipeline / service collection:

public class Startup : IWebJobsStartup
{
    public void Configure(IWebJobsBuilder builder)
    {
        //Registering a filter
        builder.Services.AddSingleton<IFunctionFilter, LogFilter>();
    }
}

Now, if you run your functions, you’ll be able to get your debug information (or whatever) written for each function at the same place:

As a good example / scenario, imagine that you can use that technique to record the execution time for each function, log the exceptions, etc. for ALL your function in one place (rather than doing it on each methods).

Just keep in mind that the Azure Functions Team has marked the filters as obsolete:

As we can see, this is just because this is a new feature and things might changed a bit later. But hey, at least, it’s working fine for now!

Happy coding 🙂


Why my Azure Media Services assets are not displayed? Display debug layers on Xamarin.iOS

Leave a Reply

Your email address will not be published. Required fields are marked *