C# Application Insights-以自定义维度中的对象名称呈现的ILogger参数
将对象作为参数传递给ilogger时,对象在Application Insights自定义维度中呈现为字符串(对象名称)。实际值未显示 注册应用程序洞察C# Application Insights-以自定义维度中的对象名称呈现的ILogger参数,c#,azure,.net-core,azure-application-insights,ilogger,C#,Azure,.net Core,Azure Application Insights,Ilogger,将对象作为参数传递给ilogger时,对象在Application Insights自定义维度中呈现为字符串(对象名称)。实际值未显示 注册应用程序洞察 services.AddApplicationInsightsTelemetry(); 新日志 public class HealthController : ControllerBase { private readonly ILogger<HealthController> _logger; public H
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);
}
}
//Log object? _smtpAppSettings.ToJson()
_logger.LogInformation("Email sent {to} {from} {subject}", to, _smtpAppSettings.From, subject)
我尝试为application insights创建一个中间件,但其值仍然是对象的名称
为什么参数不呈现为json?
编辑
好像
var health = new
{
ok = false
};
_logger.LogInformation("HEJ2 {health}", health);
有效但无效
var health = new HealthViewModel
{
ok = false
};
_logger.LogInformation("HEJ2 {health}", health);
从您的自定义维度中,我可以看出它没有将health obj参数视为额外数据
_logger.LogInformation("Hlep me pls {health}", health);
正在尝试在字符串本身中使用jsonConverter
_logger.LogInformation($"Hlep me pls {JsonConvert.SerializeObject(health)}");
不支持
引自
我想你对这件事期望太高了。它不知道JSON格式,只是在属性上调用Convert.ToString
Convert.ToString通常调用ToString(),新类的默认ToString实现只是返回类型名
你能做什么
在记录到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);
}
}
}
}
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}";
}
}
}
在启动时注册应用程序洞察和中间件
services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<ProcessApiTraceFilter>();
日志
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);
}
}
//Log object? _smtpAppSettings.ToJson()
_logger.LogInformation("Email sent {to} {from} {subject}", to, _smtpAppSettings.From, subject)
结果
是的,将其转换为json字符串是可行的,但我不想对每个日志都这样做,我在问题中提到了这一点。可以在中间件中执行此操作,但其中的值已经不正确。我认为记录的值是*.Api.Models.Health。。。etc是对象的.ToString()的值。请尝试在类中重写.ToString()并进行检查。我不建议按照此处的建议预先组合字符串,因为无法查询参数本身的值。需要解析日志消息,这可能不容易。最好按照问题中的建议,将该值作为单独的参数传递给ILogger方法。