Application Insights - аргументы ILogger, отображаемые как имя объекта в пользовательских измерениях - PullRequest
2 голосов
/ 08 марта 2020

Объекты отображаются в виде строк (имя объекта) в пользовательских измерениях Application Insights при передаче в качестве аргументов ilogger. Фактические значения не отображаются.

Регистрация приложения 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);

Ответы [ 2 ]

1 голос
/ 14 марта 2020

Не поддерживается

Цитата из https://github.com/microsoft/ApplicationInsights-dotnet/issues/1722

Я думаю, вы ожидаете слишком много от регистратора. Он не знает о формате JSON, он просто вызывает Convert.ToString для свойств

Convert.ToString обычно вызывает ToString (), и реализация ToString по умолчанию для новых классов просто возвращает имя типа

Что вы можете сделать

Используйте To Json () для объектов, зарегистрированных в ILogger, и создайте промежуточное ПО для анализа приложений и изменения имени журнала и пользовательских измерений.

Промежуточное программное обеспечение

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>();

К Json

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

0 голосов
/ 08 марта 2020

из ваших пользовательских измерений. Я вижу, что параметр obj для здоровья не рассматривается как дополнительные данные

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

при попытке использовать jsonConverter внутри самой строки.

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