gpt4 book ai didi

c# - Application Insights - ILogger 参数呈现为自定义维度中的对象名称

转载 作者:行者123 更新时间:2023-12-04 11:36:25 29 4
gpt4 key购买 nike

当作为参数传递给 ilogger 时,对象在 Application Insights 自定义维度中呈现为字符串(对象的名称)。实际值未显示。

注册应用程序洞察

services.AddApplicationInsightsTelemetry();

新日志

public class HealthController : ControllerBase
{
private readonly ILogger<HealthController> _logger;

public HealthController(ILogger<HealthController> logger)
{
_logger = logger;
}

[HttpGet]
public IActionResult Get()
{
var health = new HealthViewModel()
{
ok = false
};

_logger.LogInformation("Hlep me pls {health}", health);

return Ok(health);
}
}

结果

enter image description here

我不想对每个日志都这样做:

var health = new HealthViewModel()
{
ok = false
};

_logger.LogInformation("Hlep me pls {health}", JsonConvert.SerializeObject(health));

我尝试创建一个用于应用程序洞察的中间件,但值仍然是对象的名称..

enter image description here

为什么参数不呈现为 json?

编辑

好像是

var health = new
{
ok = false
};

_logger.LogInformation("HEJ2 {health}", health);

有效但无效

var health = new HealthViewModel
{
ok = false
};

_logger.LogInformation("HEJ2 {health}", health);

最佳答案

不支持

引自https://github.com/microsoft/ApplicationInsights-dotnet/issues/1722

I think you're expecting too much of the logger. It doesn't know about JSON format, it just calls Convert.ToString on properties

Convert.ToString typically calls ToString() and the default ToString implementation for new classes is simply to return the type name

你可以做什么

对记录到 ILogger 的对象使用 ToJson() 并创建用于应用程序洞察的中间件并修改日志名称和自定义维度。

中间件

public class ProcessApiTraceFilter : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
private readonly IIdentity _identity;
private readonly IHostEnvironment _hostEnvironment;

public ProcessApiTraceFilter(ITelemetryProcessor next, IHostEnvironment hostEnvironment, IIdentity identity)
{
Next = next;
_identity = identity;
_hostEnvironment = hostEnvironment;
}

public void Process(ITelemetry item)
{
item.Process(_hostEnvironment, _identity);

Next.Process(item);
}
}

实现

public static class ApplicationInsightsExtensions
{
public static void Process(this ITelemetry item, IHostEnvironment hostEnvironment, IIdentity identity)
{
if (item is TraceTelemetry)
{
var traceTelemetry = item as TraceTelemetry;
var originalMessage = traceTelemetry.Properties.FirstOrDefault(x => x.Key == "{OriginalFormat}");

if (!string.IsNullOrEmpty(originalMessage.Key))
{
var reg = new Regex("{([A-z]*)*}", RegexOptions.Compiled);
var match = reg.Matches(originalMessage.Value);
var formattedMessage = originalMessage.Value;
foreach (Match arg in match)
{
var parameterName = arg.Value.Replace("{", "").Replace("}", "");
var parameterValue = traceTelemetry.Properties.FirstOrDefault(x => x.Key == parameterName);
formattedMessage = formattedMessage.Replace(arg.Value, "");
}

traceTelemetry.Message = formattedMessage.Trim();
}

if (identity != null)
{
var isAuthenticated = identity.IsAuthenticated();
const string customerKey = "customer";

if (isAuthenticated && !traceTelemetry.Properties.ContainsKey(customerKey))
{
var customer = identity.Customer();

if (customer != null)
{
traceTelemetry.Properties.Add(customerKey, customer.ToJson());
}
}

var request = identity.Request();
const string requestKey = "request";

if (request != null && !traceTelemetry.Properties.ContainsKey(requestKey))
{
traceTelemetry.Properties.Add(requestKey, request.ToJson());
}
}

var applicationNameKey = "applicationName";

if (hostEnvironment != null && !string.IsNullOrEmpty(hostEnvironment.ApplicationName) && !traceTelemetry.Properties.ContainsKey(applicationNameKey))
{
traceTelemetry.Properties.Add(applicationNameKey, hostEnvironment.ApplicationName);
}
}
}
}

在启动时注册应用程序洞察和中间件

services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<ProcessApiTraceFilter>();

ToJson

public static class ObjectExtensions
{
private static readonly string Null = "null";
private static readonly string Exception = "Could not serialize object to json";

public static string ToJson(this object value, Formatting formatting = Formatting.None)
{
if (value == null) return Null;

try
{
string json = JsonConvert.SerializeObject(value, formatting);

return json;
}
catch (Exception ex)
{
return $"{Exception} - {ex?.Message}";
}
}
}

日志

//Log object? _smtpAppSettings.ToJson()

_logger.LogInformation("Email sent {to} {from} {subject}", to, _smtpAppSettings.From, subject)

结果

enter image description here

关于c# - Application Insights - ILogger 参数呈现为自定义维度中的对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60582917/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com