我正在尝试将 customDimension 添加到我的 Azure 函数日志中,但它们似乎没有通过,我不知道为什么。我尝试过使用 BeginScope 并使用单独的日志发送参数,但似乎都不起作用。
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);
}
}
}
和
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);
}
}
我发现一些线程,人们设法添加 customDimensions 但对于同一请求的所有日志 - 我只想为单个日志执行此操作,每个日志将具有不同的类型,以便可以轻松过滤它们。
功能应用程序设置如下:
.ConfigureServices((hostContext, services) =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
我正在使用最新的
Microsoft.ApplicationInsights.WorkerService
和 Microsoft.Azure.Functions.Worker.ApplicationInsights
包。
我尝试了以下 Http 触发器函数隔离模型,以使用 Application Insights 获取 Azure Function 应用程序的
Custom Dimensions
。
Helpers/LoggerExtensions.cs:
using FunctionApp24.Functions;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.Logging;
using System;
namespace FunctionApp24.Helpers
{
public static class LoggerExtensions
{
public static void LogCustomTrace(this TelemetryClient telemetryClient,
string method,
string message,
TraceType traceType,
LogLevel logLevel = LogLevel.Information,
Exception? exception = null)
{
var traceTelemetry = new TraceTelemetry
{
Message = $"[{method}]: {message}",
SeverityLevel = logLevel.ToSeverityLevel()
};
traceTelemetry.Properties["TraceType"] = traceType.ToString();
if (exception != null)
{
traceTelemetry.Properties["ExceptionType"] = exception.GetType().Name;
traceTelemetry.Properties["ExceptionMessage"] = exception.Message;
}
telemetryClient.TrackTrace(traceTelemetry);
}
public static SeverityLevel ToSeverityLevel(this LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Trace => SeverityLevel.Verbose,
LogLevel.Debug => SeverityLevel.Verbose,
LogLevel.Information => SeverityLevel.Information,
LogLevel.Warning => SeverityLevel.Warning,
LogLevel.Error => SeverityLevel.Error,
LogLevel.Critical => SeverityLevel.Critical,
_ => SeverityLevel.Information
};
}
}
}
函数/MyFunction.cs:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Microsoft.ApplicationInsights;
using System.Net;
using FunctionApp24.Helpers;
namespace FunctionApp24.Functions
{
public class MyFunction
{
private readonly ILogger<MyFunction> _logger;
private readonly TelemetryClient _telemetryClient;
public MyFunction(ILogger<MyFunction> logger, TelemetryClient telemetryClient)
{
_logger = logger;
_telemetryClient = telemetryClient;
}
[Function("MyFunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("Function MyFunction triggered");
_telemetryClient.LogCustomTrace("MyFunction", "Processing request", TraceType.Request);
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync("Function executed successfully.");
return response;
}
}
public enum TraceType
{
Request,
Response,
Error
}
}
appsettings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<storageconnec>",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"APPLICATIONINSIGHTS_CONNECTION_STRING": "<connecstring>"
}
}
将上述项目发布到 Azure Function App 并在 Azure 门户中运行它,如下所示。
查询:
在 Azure 应用程序 Inisghts > Logs 中运行以下查询以获取函数应用程序的
Cunstome Dimensions
。
traces
| where customDimensions.TraceType == "Request"
| project timestamp, message, customDimensions
| order by timestamp desc
自定义尺寸输出: