Kotlin and autocollect appinsight-agent.jar

46 Views Asked by At

I've successfully managed to log using the App-Insight library with Autocollect. While my logs with SLF4J can be sent to Azure Monitor, I'm encountering an issue where logs from my Kotlin classes aren't being captured, even though I'm using the same SLF4J library. How can I solve this? Has anyone else experienced a similar problem? I'm unsure of what to do. Perhaps I could use the Tracktrace method, but I'd prefer to find a solution using Autocollect. i use appinsight-agent version 3.5.1 appinsight-core kotlin/ java 17

1

There are 1 best solutions below

1
Suresh Chikkam On

I'm encountering an issue where logs from my Kotlin classes aren't being captured, even though I'm using the same SLF4J library.

Here, whenever we deal with app insights there will be some latency to get the trace logs if the configuration is in the correct format we will get trace logs after a while.

  • Check with the below configuration.

Included dependencies for SLF4J, Logback, applicationinsights and the Azure Monitor appender for Logback build.gradle.kts :

dependencies {
    implementation("org.slf4j:slf4j-api:2.0.0-alpha1")
    implementation("ch.qos.logback:logback-classic:1.2.3")
    implementation("com.microsoft.azure:azure-logback-appender:1.1.1")
    implementation("com.microsoft.azure:applicationinsights-agent:3.4.19")
}
  • Create a logback.xml file in the src directory.

logback.xml :

<configuration>

    <appender name="azure" class="com.microsoft.azure.logback.AzureAppender">
        <instrumentationKey>YOUR_INSTRUMENTATION_KEY</instrumentationKey>
        <contextField>properties</contextField>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="azure"/>
    </root>

</configuration>
  • Use the API provided by the SDK to track telemetry data, such as events, traces, exceptions, and performance counters.
import com.microsoft.applicationinsights.TelemetryClient

fun main() {
    val telemetryClient = TelemetryClient()
    
    // Track an event
    telemetryClient.trackEvent("MyEvent")
    
    // Track a trace
    telemetryClient.trackTrace("MyTrace")
    
    // Track an exception
    val exception = Exception("Something went wrong")
    telemetryClient.trackException(exception)
    
    // Flush telemetry data
    telemetryClient.flush()
}

Traces :

enter image description here

enter image description here