Skip to content

Commit 356aaa7

Browse files
committed
Added dedicated module for azure functions
1 parent f0c3451 commit 356aaa7

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.2.0" />
10+
<PackageReference Include="Microsoft.DurableTask.Client" Version="1.16.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\CoreHelpers.Extensions.Logging.Tasks\CoreHelpers.Extensions.Logging.Tasks.csproj" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using CoreHelpers.Extensions.Logging.Tasks;
2+
using CoreHelpers.TaskLogging;
3+
using Microsoft.DurableTask;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace CoreHelpers.Extensions.Logging.DurableTask
7+
{
8+
public static class DurableTaskClientExtension
9+
{
10+
public static ITaskLoggerScope? BeginOrchestratorTaskLoggerContext(this ILogger logger, TaskOrchestrationContext context, ITaskLoggerFactory taskLoggerFactory)
11+
{
12+
// lookup the task log id
13+
var tasklogId = taskLoggerFactory.LookupTaskIdByExternalId(context.InstanceId).GetAwaiter().GetResult();
14+
if (string.IsNullOrEmpty(tasklogId))
15+
{
16+
tasklogId = taskLoggerFactory.AnnounceTask(context.Name, context.Parent == null ? "OrchestratorInvocation" : "SubOrchestratorInvocation", "AzureFunctionsFlexConsumption").GetAwaiter().GetResult();
17+
taskLoggerFactory.RegisterExternlIdForTask(tasklogId, context.InstanceId).GetAwaiter().GetResult();
18+
}
19+
20+
// return the context
21+
return logger.BeginTaskScope(tasklogId);
22+
}
23+
}
24+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using CoreHelpers.Extensions.Logging.Tasks;
5+
using Microsoft.Azure.Functions.Worker;
6+
using Microsoft.Azure.Functions.Worker.Builder;
7+
using Microsoft.Azure.Functions.Worker.Middleware;
8+
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace CoreHelpers.Extensions.Logging.DurableTask
12+
{
13+
public static class FunctionsApplicationBuilderExtensions
14+
{
15+
public static FunctionsApplicationBuilder UseTaskLoggingMiddleware(this FunctionsApplicationBuilder builder)
16+
{
17+
builder.UseMiddleware<InvocationTaskLoggingMiddleware>();
18+
return builder;
19+
}
20+
}
21+
22+
internal class InvocationTaskLoggingMiddleware : IFunctionsWorkerMiddleware
23+
{
24+
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
25+
{
26+
// figure out if it is an orchestration function
27+
bool isOrchestration = context.FunctionDefinition.InputBindings.Values
28+
.Any(b => b.Type.EndsWith("orchestrationTrigger", StringComparison.OrdinalIgnoreCase));
29+
30+
if (isOrchestration)
31+
{
32+
await next(context);
33+
}
34+
else
35+
{
36+
var logger = context.GetLogger(context.FunctionDefinition.Name);
37+
38+
using (logger.BeginNewTaskScope(context.FunctionDefinition.Name, "ActivityInvocation", "AzureFunctionsFlexConsumption"))
39+
{
40+
try
41+
{
42+
await next(context);
43+
}
44+
catch (Exception ex)
45+
{
46+
logger.LogError(ex, "An unhandled exception occurred during function invocation.");
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}

dotNet/TaskLogging.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreHelpers.TaskLogging.Sam
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreHelpers.Extensions.Logging.Tasks", "CoreHelpers.Extensions.Logging.Tasks\CoreHelpers.Extensions.Logging.Tasks.csproj", "{F6BBD6CC-795E-47D4-8F2C-27D4BC14E78F}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreHelpers.Extensions.Logging.DurableTask", "CoreHelpers.Extensions.Logging.DurableTask\CoreHelpers.Extensions.Logging.DurableTask.csproj", "{56E27924-4E2F-488C-BEFF-BC5AB91CCF8B}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{F6BBD6CC-795E-47D4-8F2C-27D4BC14E78F}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{F6BBD6CC-795E-47D4-8F2C-27D4BC14E78F}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{F6BBD6CC-795E-47D4-8F2C-27D4BC14E78F}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{56E27924-4E2F-488C-BEFF-BC5AB91CCF8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{56E27924-4E2F-488C-BEFF-BC5AB91CCF8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{56E27924-4E2F-488C-BEFF-BC5AB91CCF8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{56E27924-4E2F-488C-BEFF-BC5AB91CCF8B}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)