Azure

Exporting OpenTelemetry Logs to Azure Monitor utilizing ASP.NET Core

OpenTelemetry is an observability framework that gives APIs, libraries, brokers, and instrumentation to gather distributed traces and metrics from an utility. It is an open-source, CNCF venture that goals to standardize the era and assortment of telemetry knowledge (metrics, logs, and traces) for cloud-native software program.

On this article, you’ll learn to export OpenTelemetry utility logs out of your ASP.NET Core purposes to Azure Monitor.

Listed below are the steps to comply with alongside.

  • Substitute the contents of the Program.cs file with the next code.
    utilizing Azure.Monitor.OpenTelemetry.AspNetCore;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add the Azure Monitor telemetry service to the applying.
    // This service will gather and ship telemetry knowledge to Azure Monitor.
    builder.Providers.AddOpenTelemetry().UseAzureMonitor();
    
    // Construct the ASP.NET Core net utility.
    var app = builder.Construct();
    
    app.MapGet("/logs-demo", (ILogger<Program> logger) =>
    {
        // You possibly can ship structured log messages to Azure Monitor as effectively.
        logger.LogInformation("That is an data log. Consumer: {username}", "dummy person");
        logger.LogWarning("It is a warning log");
        logger.LogError("That is an error log");
        return "Logs generated!";
    });
    
    // Run the applying.
    app.Run();
    

The above code snippet demonstrates methods to arrange an ASP.NET Core net utility with OpenTelemetry and Azure Monitor integration for the aim of gathering and exporting telemetry knowledge. Let’s break down the code to grasp the way it works.

To configure OpenTelemetry logging within the code, we use the AddOpenTelemetry technique on the ILoggingBuilder occasion. This technique provides the OpenTelemetry logging supplier to our utility. We then take away all different logging suppliers from our utility utilizing the ClearProviders technique. This ensures that solely OpenTelemetry is used for logging.

Lastly, we invoke the AddOpenTelemetry technique with a lambda that configures the OpenTelemetry logging. Inside this lambda, we use the AddAzureMonitorLogExporter technique to attach the Azure Monitor exporter to our logging pipeline.

So as to add the OpenTelemetry service and configure it to make use of Azure Monitor, we use the AddOpenTelemetry().UseAzureMonitor() chain of strategies. This service collects and sends all telemetry knowledge to Azure Monitor.

With the intention to check the implementation, we created an endpoint at “/logs-demo”. This endpoint is liable for producing three various kinds of log messages, every with a definite severity stage: data, warning, and error. To realize this, we used the ILogger<Program> occasion that was injected into the tactic. To reveal structured logging, we created a structured log message as the primary log message. As soon as the logs have been generated, the endpoint returns a message that claims “Logs generated!”.

We’re able to debug our utility now, however earlier than that, we’ve got to configure the connection string for the Software Insights useful resource that we created earlier. To perform this, set the APPLICATIONINSIGHTS_CONNECTION_STRING atmosphere variable worth to the connection string that you just copied beforehand. You are able to do this by working the next command within the terminal:

APPLICATIONINSIGHTS_CONNECTION_STRING=<connection-string>

Output

Debug the applying and invoke the endpoint at /logs-demo. Each profitable invocation of the endpoint will generate three log messages with totally different severity ranges every of which will likely be exported to Azure Monitor.

To view the exported logs, go to the Azure Portal and navigate to the Software Insights useful resource that you just created earlier. Click on on the Logs tab and run the next question:

traces

The next screenshot exhibits the output of the question.

Click on on the structured log message to view the main points of the log message. You’ll discover that the log message comprises the username property as a customized dimension you could later use to filter the logs.

 Username property

To discover the mixing between Azure Monitor and OpenTelemetry additional, together with the varied forms of metrics that may be exported, please check with the official documentation.

Know extra about our firm at Skrots. Know extra about our companies at Skrots Providers, Additionally checkout all different blogs at Weblog at Skrots

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button