1

We are using spring boot to send metrics to app insight we are using applicationinsights-logging-log4j2.

Below are the appenders we are using in logj2-spring.xml

*

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{MM-dd-yyyy'T'HH:mm:ss.SSS,UTC} %correlationId [%thread] %-5level %logger{36}- %msg%n"/>
    </Console>
    <ApplicationInsightsAppender name="aiAppender">
    </ApplicationInsightsAppender>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="Console"  />
      <AppenderRef ref="aiAppender"  />
    </Root>
  </Loggers>

We are seeing the logs in app insight search screen however i have few questions.

  1. Is there a way to define a custom info in logging like correlationId(guid used to track a flow uniquely) and send it to AI just like we are appending in console logs.

  2. Is there anything like pattern we can define for AI.

  3. Is there a use of console appender and logging to console if we are logging to AI.

pvjhs
  • 549
  • 1
  • 9
  • 24
  • maybe this is helpful : https://dzone.com/articles/correlation-id-for-logging-in-microservices – granadaCoder Jul 28 '20 at 21:13
  • @granadaCoder Yes i am doing the same for console logs where i can define the pattern , but i dont find the pattern to be provided in App Insight logger that is where my question is – pvjhs Jul 29 '20 at 05:15

1 Answers1

1
  1. You can create a class that will extend the OncePerRequestFilter, and in that class generate one Id using UUID generator and set this UUID in variable, let's say RequestId. And then write MDC.put('requestid', RequestId).
    OncePerRequestFilter class is executed with every HTTP request, you won't be required to call the class extending it explicitly, and MDC.put('requestid', RequestId) will be added as external property in your application insight log.

This is just a workaround for correlationid though it is providing us a same feature, that we can aggregate a log. Whatever requestid is being generated, you can retrieve that and then use it application insight to see logs for that request.

  1. I believe console appender is still helpful, because I. AI we can see loga after at least 4 to 5 mins, so for real time debugging console logs are helpful. Though you can. Configure what type of logs you want to see in console and what you wanna sent to ai.

Function that will set the value of request Id in MDC, then this can be called from the entry point of all type of request.

Nadeem Khan
  • 104
  • 1
  • 6
  • I need to log the correlationId in http call as well as jms/kafka/eventhub listeners log, I beleive filter approach will work for only http call? – pvjhs Jul 29 '20 at 05:54
  • I completely agree. But I believe if we externalize that MDC logic in a separate class, and then use it in the very first class or function that is being invoked for JMS/Kafka/EventHub/Http. Then we will be able to achieve the correlationId for all type of request. Because the idea is to put the value of requested in MDC, and it will maintain its value until it is updated some other process. – Nadeem Khan Jul 29 '20 at 07:33
  • I completely got your idea of adding things in MDC, what I am confused about , whatever is there in MDC, will automatically go in azure app insight logs? beacuse while logging in console we need to specify things which are getting logged in pattern of appender eg i am mentioning correlationId in console appender , Such pattern also exists/valid for app insight , if not how to put extra fields in logs ? – pvjhs Jul 29 '20 at 08:24
  • I believe Application Insight Appender doesn't have setEncoder method implemented, that's why we are unable to add Pattern to messages. If setEncoder would have been implemented, then we could have easily used http://logback.qos.ch/manual/layouts.html (Chapter 6 of logback, in this you need to look for PatternLayout example). But if somehow we can implement the setEncoder method, then we can easily set the layout for messages. – Nadeem Khan Jul 29 '20 at 13:38