java项目日志写入application insights

苍嘉澍
2023-12-01

1、添加HTTP跟踪
参考文档:https://docs.microsoft.com/en-us/azure/application-insights/app-insights-java-get-started#3-add-an-application-insights-xml-file

2、添加application insights log4j adapter
添加jar包,调用Api,往application insights存储trace
参考文档:https://docs.microsoft.com/en-us/azure/application-insights/app-insights-java-trace-logs

3、代码案例参考
文档案例:
https://github.com/Microsoft/ApplicationInsights-Java/blob/master/collectd/src/main/java/com/microsoft/applicationinsights/collectd/ApplicationInsightsWriter.java

4、Azure平台相关文档
参考文档:https://docs.microsoft.com/zh-cn/azure/application-insights/app-insights-api-custom-events-metrics

import com.microsoft.applicationinsights.TelemetryClient;
TelemetryClient appInsights = new TelemetryClient();
appInsights.trackTrace("application insights log4j adapter444444");
//异常处理
appInsights.trackException(e);

若不想变更过多的代码,可以封装一层,方法和log4f同名

package com.bmw.browser.id5.cn.share.loggerAzure;

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.telemetry.SeverityLevel;
import com.microsoft.applicationinsights.telemetry.TelemetryContext;

/**
 * Created by qxr4383 on 2017/12/22.
 */
public class ApplicationInsights {
    private static TelemetryClient telemetryClient = null;
    private static ApplicationInsights applicationInsights = null;
    private ApplicationInsights(TelemetryClient telemetryClient){
        this.telemetryClient = telemetryClient;
    }

    public static ApplicationInsights getApplicationInsights(){
        initializeApplicationInsights();
        return applicationInsights;
    }
//    public static TelemetryClient getTelemetryClient() {
//        initializeTelemetryClient();
//        return telemetryClient;
//    }
//
//    private static void initializeTelemetryClient() {
//        if (telemetryClient==null) {
//            synchronized (ApplicationInsights.class) {
//                if (telemetryClient==null) {
//                    telemetryClient = new TelemetryClient();
//                }
//            }
//        }
//    }

    private static void initializeApplicationInsights() {
        if (applicationInsights==null) {
            synchronized (ApplicationInsights.class) {
                if (applicationInsights==null) {
                //设置InstrumentationKey
                    TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.getActive();                   telemetryConfiguration.setInstrumentationKey(System.getenv().get("APPLICATION_INSIGHTS_IKEY"));
                    telemetryClient = new TelemetryClient(telemetryConfiguration);
                    applicationInsights = new ApplicationInsights(telemetryClient);
                }
            }
        }
    }

    public static void debug(String message){
        telemetryClient.trackTrace(message, SeverityLevel.Verbose);
    }

    public static void info(Object object){
        if(object!=null){
            telemetryClient.trackTrace(object.toString());
        }
    }

    public static void info(String message){
        telemetryClient.trackTrace(message);
    }

    public static void trackEvent(String message) {
        telemetryClient.trackEvent(message);
    }

    public static void warn(String message){
        telemetryClient.trackTrace(message, SeverityLevel.Warning);
    }

    public static void warn(String message,Exception e){
        telemetryClient.trackException(e);
    }
    public static void error(String message){
        telemetryClient.trackTrace(message, SeverityLevel.Error);
    }

    public static void error(String message,Exception e){
        telemetryClient.trackException(e);
    }
}
 类似资料: