在Spring Boot App中使用ApplicationInsights进行日志记录

时间:2020-07-26 06:54:58

标签: azure spring-boot azure-application-insights

我们正在使用Spring Boot将指标发送到应用程序见解,而我们正在使用applicationinsights-logging-log4j2。

下面是我们在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>

我们在“应用洞察力搜索”屏幕中看到了日志,但是我有几个问题。

  1. 是否有一种方法可以在日志记录中定义一个自定义信息,例如correlationId(用于唯一跟踪流的guid),并将其发送给AI,就像我们要添加到控制台日志中一样。

  2. 我们可以为AI定义类似模式的东西吗?

  3. 如果我们要登录到AI,是否可以使用控制台附加程序并登录到控制台。

1 个答案:

答案 0 :(得分:1)

  1. 您可以创建一个将扩展OncePerRequestFilter的类,并在该类中使用UUID生成器生成一个ID,然后将此UUID设置为变量,即RequestId。 然后编写MDC.put('requestid',RequestId)。
    一旦对每个HTTP请求执行了PerRequestFilter类,就不需要调用该类显式扩展它,并且MDC.put('requestid',RequestId)将作为外部属性添加到您的应用程序洞察日志中。

这只是correlationidid的一种解决方法,尽管它为我们提供了可以聚合日志的相同功能。无论生成什么requestid,您都可以检索该id,然后使用它的应用程序洞察力查看该请求的日志。

  1. 我相信控制台附加程序仍然有用,因为I. AI至少4至5分钟后我们可以看到loga,因此对于实时调试控制台日志很有帮助。虽然可以。配置您要在控制台中查看的日志类型以及要发送给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.

相关问题