Validate the Crash Analysis feature

更新时间:
复制 MD 格式

Preparations

Product features

1. Custom Exception

The Crash Analysis SDK provides an API to add custom exceptions.

import com.aliyun.emas.apm.crash.ApmCrashAnalysis

ApmCrashAnalysis.getInstance()
  .recordException(RuntimeException("This is an non-fatal"))
import com.aliyun.emas.apm.crash.ApmCrashAnalysis;

ApmCrashAnalysis.getInstance()
  .recordException(new RuntimeException("This is an non-fatal"));

For more information about the API, see Custom Exception.

2. Add custom dimensions (Optional)

The Crash Analysis SDK provides an API to set key-value pairs. You can use this API to add extra information to crash logs as needed.

import com.aliyun.emas.apm.crash.ApmCrashAnalysis

ApmCrashAnalysis.getInstance().setCustomKey(key, value)
import com.aliyun.emas.apm.crash.ApmCrashAnalysis;

ApmCrashAnalysis.getInstance().setCustomKey(key, value);

For more information about the API, see Add custom dimensions.

3. Set log information (Optional)

The Crash Analysis SDK provides an API to set log information. You can use this API to add operational log data to crash logs as needed.

import com.aliyun.emas.apm.crash.ApmCrashAnalysis

ApmCrashAnalysis.getInstance().log(message)
import com.aliyun.emas.apm.crash.ApmCrashAnalysis;

ApmCrashAnalysis.getInstance().log(message);

For more information about the API, see Add log information.

Feature validation

1. Force a test crash

To view crash data in the EMAS console, you can force a test crash.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val crashButton = Button(this)
    crashButton.setText("Test Crash")
    crashButton.setOnClickListener(object : View.OnClickListener {
        override fun onClick(view: View?) {
            throw RuntimeException("Test Crash") // Trigger a crash
        }
    })

    addContentView(
        crashButton, ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
    )
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    Button crashButton = new Button(this);
    crashButton.setText("Test Crash");
    crashButton.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           throw new RuntimeException("Test Crash"); // Trigger a crash
       }
    });
    
    addContentView(crashButton, new ViewGroup.LayoutParams(
           ViewGroup.LayoutParams.MATCH_PARENT,
           ViewGroup.LayoutParams.WRAP_CONTENT));
}

Build and run the app. Then, click the Test Crash button to trigger a crash.

2. View logs

Filter by the tag:Apm-CrashAnalysis tag to check whether the crash data is reported. The following snippet shows the key logs.

 # Start reporting crash data
 D  Sending report : 67AC2CAD026A00016CB8DE472237D11C
 # Crash data reported successfully
 D  Crashlytics report successfully enqueued to DataTransport: 67AC2CAD026A00016CB8DE472237D11C

3. View data in the console

Go to the console and select your project. Follow the steps in the following figure to view the crash log.