Pietro
Pietro

Reputation: 781

How to set Scope in Application Insight for Azure Function?

When I use Application Insight in an Azure Function it looks like

using (_logger.BeginScope("MyLog")) 
{
 _logger.LogInformation("Test");
}

has no effects, i.e. the log appears in Application Insight but it doens't contains any scope in customDimensions. The same exact code works fine in a web api project.

UPDATE:

If I try to set the scope with

using (_logger.BeginScope(new Dictionary<string, object> { ["Scope"] = "MyLog" }))
{
 _logger.LogInformation("Test");
}

I can see the scope set in customDimensions but the name is: prop__Scope instead of just Scope

Is this an undocumented behaviour or am I missing something?

Upvotes: 1

Views: 1726

Answers (1)

cijothomas
cijothomas

Reputation: 3126

I can see the scope set in customDimensions but the name is: prop__Scope instead of just Scope

"The prop__ prefix is added to ensure there are no collisions between fields the runtime adds and fields your function code adds."

This is the documented behavior for Azure Functions. See

Structured Logging (Develop C# class library functions using Azure Functions | Microsoft Learn)

Upvotes: 2

Related Questions