在 Application Insights 中浏览 Java 跟踪日志Explore Java trace logs in Application Insights

如果使用 Logback 或 Log4J(v1.2 或 v2.0)进行跟踪,可将跟踪日志自动发送到 Application Insights,以便在其中发现和搜索日志。If you're using Logback or Log4J (v1.2 or v2.0) for tracing, you can have your trace logs sent automatically to Application Insights where you can explore and search on them.

提示

只需为应用程序设置一次 Application Insights 检测密钥。You only need to set your Application Insights Instrumentation Key once for your application. 如果使用的是 Java Spring 之类的框架,则可能已经在应用配置中的其他位置注册了密钥。If you are using a framework like Java Spring, you may have already registered the key elsewhere in your app's configuration.

使用 Application Insights Java 代理Using the Application Insights Java agent

默认情况下,Application Insights Java 代理会自动捕获在 WARN 级别和更高级别上执行的日志记录。By default, the Application Insights Java agent automatically captures logging performed at WARN level and above.

可以更改使用 AI-Agent.xml 文件捕获的日志记录的阈值:You can change the threshold of logging that is captured using the AI-Agent.xml file:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsightsAgent>
   <Instrumentation>
      <BuiltIn>
         <Logging threshold="info"/>
      </BuiltIn>
   </Instrumentation>
</ApplicationInsightsAgent>

可以使用 AI-Agent.xml 文件禁用 Java 代理的日志记录捕获:You can disable the Java agent's logging capture using the AI-Agent.xml file:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsightsAgent>
   <Instrumentation>
      <BuiltIn>
         <Logging enabled="false"/>
      </BuiltIn>
   </Instrumentation>
</ApplicationInsightsAgent>

或者(不使用 Java 代理),可以按照以下说明进行操作Alternatively (as opposed to using the Java agent), you can follow the instructions below

安装 Java SDKInstall the Java SDK

按照说明安装适用于 Java 的 Application Insights SDK(如果尚未安装)。Follow the instructions to install Application Insights SDK for Java, if you haven't already done that.

将日志记录库添加到项目Add logging libraries to your project

为项目选择适当的方式。Choose the appropriate way for your project.

如果使用 Maven...If you're using Maven...

如果项目已设置为使用 Maven 进行生成,请将以下代码片段之一合并到 pom.xml 文件。If your project is already set up to use Maven for build, merge one of the following snippets of code into your pom.xml file.

然后刷新项目依赖项以下载二进制文件。Then refresh the project dependencies, to get the binaries downloaded.

LogbackLogback


    <dependencies>
       <dependency>
          <groupId>com.microsoft.azure</groupId>
          <artifactId>applicationinsights-logging-logback</artifactId>
          <version>[2.0,)</version>
       </dependency>
    </dependencies>

Log4J v2.0Log4J v2.0


    <dependencies>
       <dependency>
          <groupId>com.microsoft.azure</groupId>
          <artifactId>applicationinsights-logging-log4j2</artifactId>
          <version>[2.0,)</version>
       </dependency>
    </dependencies>

Log4J v1.2Log4J v1.2


    <dependencies>
       <dependency>
          <groupId>com.microsoft.azure</groupId>
          <artifactId>applicationinsights-logging-log4j1_2</artifactId>
          <version>[2.0,)</version>
       </dependency>
    </dependencies>

如果使用 Gradle...If you're using Gradle...

如果项目已设置为使用 Gradle 进行生成,请将以下代码行之一添加到 build.gradle 文件中的 dependencies 组。If your project is already set up to use Gradle for build, add one of the following lines to the dependencies group in your build.gradle file:

然后刷新项目依赖项以下载二进制文件。Then refresh the project dependencies, to get the binaries downloaded.

LogbackLogback


    compile group: 'com.microsoft.azure', name: 'applicationinsights-logging-logback', version: '2.0.+'

Log4J v2.0Log4J v2.0

    compile group: 'com.microsoft.azure', name: 'applicationinsights-logging-log4j2', version: '2.0.+'

Log4J v1.2Log4J v1.2

    compile group: 'com.microsoft.azure', name: 'applicationinsights-logging-log4j1_2', version: '2.0.+'

否则...Otherwise ...

按照指南手动安装 Application Insights Java SDK、下载相应追加器的 jar(到达“Maven 中心”页后,单击“下载”部分中的“jar”链接),并将下载的追加器 jar 添加到项目。Follow the guidelines to manually install Application Insights Java SDK, download the jar (After arriving at Maven Central Page click on 'jar' link in download section) for appropriate appender and add the downloaded appender jar to the project.

记录器Logger 下载Download Library
LogbackLogback Logback 追加器 JarLogback appender Jar applicationinsights-logging-logbackapplicationinsights-logging-logback
Log4J v2.0Log4J v2.0 Log4J v2 追加器 JarLog4J v2 appender Jar applicationinsights-logging-log4j2applicationinsights-logging-log4j2
Log4j v1.2Log4j v1.2 Log4J v1.2 追加器 JarLog4J v1.2 appender Jar applicationinsights-logging-log4j1_2applicationinsights-logging-log4j1_2

将追加器添加到日志记录框架Add the appender to your logging framework

要开始跟踪,请将相关的代码片段合并到 Log4J 或 Logback 配置文件:To start getting traces, merge the relevant snippet of code to the Log4J or Logback configuration file:

LogbackLogback


    <appender name="aiAppender" 
      class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender">
        <instrumentationKey>[APPLICATION_INSIGHTS_KEY]</instrumentationKey>
    </appender>
    <root level="trace">
      <appender-ref ref="aiAppender" />
    </root>

Log4J v2.0Log4J v2.0


    <Configuration packages="com.microsoft.applicationinsights.log4j.v2">
      <Appenders>
        <ApplicationInsightsAppender name="aiAppender" instrumentationKey="[APPLICATION_INSIGHTS_KEY]" />
      </Appenders>
      <Loggers>
        <Root level="trace">
          <AppenderRef ref="aiAppender"/>
        </Root>
      </Loggers>
    </Configuration>

Log4J v1.2Log4J v1.2


    <appender name="aiAppender" 
         class="com.microsoft.applicationinsights.log4j.v1_2.ApplicationInsightsAppender">
        <param name="instrumentationKey" value="[APPLICATION_INSIGHTS_KEY]" />
    </appender>
    <root>
      <priority value ="trace" />
      <appender-ref ref="aiAppender" />
    </root>

Application Insights 追加器可由配置的任何记录器(而不一定是根记录器)引用(如上面的代码示例所示)。The Application Insights appenders can be referenced by any configured logger, and not necessarily by the root logger (as shown in the code samples above).

在 Application Insights 门户中浏览跟踪Explore your traces in the Application Insights portal

将项目配置为向 Application Insights 发送跟踪后,可以在 Application Insights 门户的搜索边栏选项卡中查看和搜索这些跟踪。Now that you've configured your project to send traces to Application Insights, you can view and search these traces in the Application Insights portal, in the Search blade.

通过记录器提交的异常将作为异常遥测显示在门户中。Exceptions submitted via loggers will be displayed on the portal as Exception Telemetry.

在 Application Insights 门户中打开“搜索”

后续步骤Next steps

诊断搜索Diagnostic search