Search code examples
c#azureazure-application-insightsilogger

where are the properties of Custom object in app insights


i have a custom object like this.

 public class Propertey
{
    public string Key { get; set; }
    public string Value { get; set; }
}
public class MonitoringEvent 
{
    public string AppName { get; set; }
    public string Service { get; set; }
    public string Fuction { get; set; }
    public string CorrelationId { get; set; }
    public List<Propertey> Properties { get; set; }
    public string EventName { get; set; }
    public DateTime TimeStamp { get; set; } = DateTime.UtcNow;
}

and this object gets populated from outside the system and in my function app i am trying to log it in App Insights.

     [FunctionName("EventMonitoring")]
    public async Task Run([ServiceBusTrigger(
            "cta-event-monitoring",
            "monitoring",
            Connection = "ServiceBusConnectionString",
            IsSessionsEnabled = false)]string mySbMsg, ILogger log)
    {
        try
        {
            MonitoringEvent me = JsonConvert.DeserializeObject<MonitoringEvent>(mySbMsg);
           
            log.LogInformation("MonitoringEvent", me);
        }
        catch (Exception ex)
        {

            log.LogError("MonitoringEventError",ex);
        }
    }

when i see App insights i cant see properties like "AppName" "Service" etc in app insights. where can i find them ? later i want to be able to query on them as well. but all i see in app insights is something like this

enter image description here


Solution

  • You need to log using a log message template. The message template can contain placeholders for which arguments are provided. Use names for the placeholders. In your case that means you need to add a named template position

    log.LogInformation("MonitoringEvent {Event}", me);

    This will create a custom property "Event" in the trace of application insights.

    In your current code you do not provide a placeholder so the argument cannot be put somewhere and is ignored.

    Also, do mind that application insights will probably use .ToString() on any object arguments so your best bet will be to just use mySbMsg as the argument.