美文网首页
Application Insights 集成

Application Insights 集成

作者: 暗夜行者 | 来源:发表于2020-10-12 16:54 被阅读0次

先复习一下常用的log框架。

SLF4J是一个标准的API集合,log4j实现了它的接口,log4j已经在2015年停用,它有两个衍生版本,Logback 和 Log4j2,二者皆自动支持SLF4J,区别为log4j2更新潮,可以说是基于log4j和logback的升级版。

参考:https://stackify.com/compare-java-logging-frameworks/

AppInsight 可以使用TelemetryClient来记录event等,也可以直接对log4j和logback进行集成,以下为对logback进行集成的方式:

1.pom.xml文件加入:

<dependencies>

  <dependency>

            <groupId>com.microsoft.azure</groupId>

            <artifactId>applicationinsights-spring-boot-starter</artifactId>

            <version>2.4.0</version>

        </dependency>

      <dependency>

          <groupId>com.microsoft.azure</groupId>

          <artifactId>applicationinsights-logging-logback</artifactId>

          <version>[2.0,)</version>

      </dependency>

    </dependencies>

2.appplication configure 文件加入:

# Specify the instrumentation key of your Application Insights resource.azure.application-insights.instrumentation-key=[your ikey from the resource]

# Specify the name of your springboot application. This can be any logical name you would like to give to your app.spring.application.name=[your app name]

3.logback.xml 配置加入:

<springProperty scope="context" name="INSTRUMENTATION_KEY"

                        source="azure.application-insights.instrumentation-key"/>

<appender name="aiAppender"

                  class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender">

            <instrumentationKey>${INSTRUMENTATION_KEY}</instrumentationKey>

 </appender>

<root level="INFO">

      <appender-ref ref="aiAppender" />

</root>

得到结果查看:

参考: https://docs.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-java-applicationinsights

https://docs.microsoft.com/en-us/azure/azure-monitor/app/java-trace-logs

https://blog.akquinet.de/2019/06/14/logging-with-azure-application-insights-part-1-spring-boot/

https://github.com/microsoft/ApplicationInsights-Java/issues/880

相关文章

网友评论

      本文标题:Application Insights 集成

      本文链接:https://www.haomeiwen.com/subject/zevfuktx.html