Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangengzheng/5641303d6ab54f61ac21633f2df6900e to your computer and use it in GitHub Desktop.
Save wangengzheng/5641303d6ab54f61ac21633f2df6900e to your computer and use it in GitHub Desktop.
FunctionAppLoggingInterception

FunctionAppLoggingInterception

Summary

Demonstrates a simple logging interceptor using Functionless built on Autofac.

Usage

Invoke the report job via the built-in Functionless orchestrator ...

POST /api/orchestrator?$method=ReportJob.<ExecuteAsync>()

Or by adding and invoking a custom tirgger, for example ...

[FunctionName("reportjob-execute")]
public async Task Execute(
    [HttpTrigger] HttpRequest request,
    [DurableClient] IDurableOrchestrationClient client)
{
    await client.DurablyInvokeAsync(
        async () => await this.reportJob.ExecuteAsync()
    );
}
POST /api/reportjob-execute
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Functionless" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
{
"version": "2.0",
"logging": {
"logLevel": {
"FunctionAppLogingInterception": "Debug"
},
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
using System;
using Microsoft.Extensions.Logging;
using Castle.DynamicProxy;
namespace FunctionAppLogingInterception
{
public class LoggingInterceptor : IInterceptor
{
private Func<ILoggerFactory> loggerFactoryFactory;
public LoggingInterceptor(Func<ILoggerFactory> loggerFactoryFactory)
{
this.loggerFactoryFactory = loggerFactoryFactory;
}
public void Intercept(IInvocation invocation)
{
this.loggerFactoryFactory()
.CreateLogger(invocation.Method.DeclaringType)
.LogDebug("{Method} invoked", invocation.Method);
invocation.Proceed();
}
}
}
using System.Linq;
using System.Threading.Tasks;
using Functionless.Durability;
namespace FunctionAppLogingInterception
{
public class ReportJob
{
[NewOrchestration]
public virtual async Task ExecuteAsync()
{
await this.GenerateReportsAsync();
}
[SubOrchestration]
public virtual async Task GenerateReportsAsync()
{
await Task.WhenAll(
Enumerable.Range(0, 3).Select(_ => this.GenerateReportAsync()).ToArray()
);
}
[Activity]
public virtual async Task GenerateReportAsync()
{
Enumerable.Range(0, 100000000).Select(p => (long)p).Sum();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment