I am trying to add a customDimension to my Azure Function logs but they don't seem to be coming through and I have no idea why. I have tried using BeginScope and sending the args with the individual logs but neither seems to work.
public static void LogCustomTrace(this ILogger logger,
string method,
string message,
TraceType traceType,
LogLevel logLevel = LogLevel.Information,
Exception? exception = null,
params object[] args)
{
var customProperties = new Dictionary<string, object>
{
["TraceType"] = traceType.ToString()
};
using (logger.BeginScope(customProperties))
{
if (exception != null)
{
logger.Log(logLevel, exception, $"[{method}]: {message}", args);
}
else
{
logger.Log(logLevel, $"[{method}]: {message}", args);
}
}
}
and
public static void LogCustomTrace(this ILogger logger,
string method,
string message,
TraceType traceType,
LogLevel logLevel = LogLevel.Information,
Exception? exception = null,
params object[] args)
{
var customProperties = new Dictionary<string, object>
{
["TraceType"] = traceType.ToString()
};
if (exception != null)
{
logger.Log(logLevel, exception, $"[{method}]: {message}", customProperties);
}
else
{
logger.Log(logLevel, $"[{method}]: {message}", customProperties);
}
}
I have found some threads where people have managed to add customDimensions but for all logs for the same request - I just want to do it for a single log and each log will have a different type so they can be easily filtered.
The function app is set up like so:
.ConfigureServices((hostContext, services) =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
and I'm using the up to date Microsoft.ApplicationInsights.WorkerService and Microsoft.Azure.Functions.Worker.ApplicationInsights pacakges.

