pvjhs
pvjhs

Reputation: 699

Logging with applicationinsights in spring boot app

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.

Upvotes: 1

Views: 1971

Answers (1)

Nadeem Khan
Nadeem Khan

Reputation: 174

  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.

Upvotes: 1

Related Questions