10 Getting Started for .NET Core Applications
Saar Shen редактировал(а) эту страницу 2018-09-11 09:51:57 -07:00

This page is intended to be the Getting Started page for setting up application insights for .NET Core application. If you are interested in getting started with ASP.NET Core Application, please refer Getting Started. If you are not using Kubernetes but still want to make use of Application Insights with .NET Core application, refer this wiki.

Walkthrough

Create Application Insights Resource

Please refer Create an Application Insights resource to create an application insights resource, note down the instrumentation key.

Build the App

  • Add nuget packages:

The following nuget packages are the minimum to get application insights work with your .NET Core application:

 PM> Install-Package Microsoft.ApplicationInsights.AspNetCore
 PM> Install-Package Microsoft.ApplicationInsights.Kubernetes -Version 1.0.0-*

Find the latest package on NuGet site for AspNetCore and Microsoft.ApplicationInsights.Kubernetes.

  • Write code to configure application insights:

Use the following example code to set up the application insights and enable ApplicaitonInsights.Kubernetes. You can keep TelemetryConfiguration alive until the application finishes that you can get a telemetry client anywhere you want.

Notice that we are fetching the iKey from the environment variables that we need to set it later by Kubernetes deployment.

using System.Threading;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.Configuration;

namespace Microsoft.ApplicaitonInsights.Exmaples.NetCoreConsole
{
    class Program
    {
        static int Main(string[] args)
        {
            // Adding all environment variables into IConfiguration
            IConfiguration config = new ConfigurationBuilder().AddEnvironmentVariables().Build();

            // Read instrumentation key from IConfiguration
            string iKey = config["AI_KEY"];
            // Making sure AI_KEY is set
            if (string.IsNullOrEmpty(iKey))
            {
                logger.LogCritical("Please set the environment variable of AI_KEY with a property instrumentation key.");
                return -1;
            }

            // Build the client using the iKey to send trace.
            using (TelemetryConfiguration configuration = new TelemetryConfiguration(iKey))
            {
                TelemetryClient client = new TelemetryClient(configuration);

                // Enable kubernetes module
                Microsoft.ApplicationInsights.Kubernetes.KubernetesModule.EnableKubernetes(configuration, loggerFactory);

                while (true)
                {
                    // Send the trace
                    client.TrackTrace("Hello from AI SDK");
                    client.Flush();
                    Thread.Sleep(30000);
                }
            }
        }
    }
}

Containerize the application

For example, use the following Dockerfile:

FROM microsoft/dotnet:1.1-runtime
ARG source
WORKDIR /app
COPY ${source:-./bin/Debug/netcoreapp1.1/publish .
ENTRYPOINT ["dotnet", "netcore-console.dll"]

You'll need to push it to a container registry once you have the container built.

Deploy it

For example, use the yaml to deploy the application to Kubernetes:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: saars-u-service
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: u-svc-demo-2
    spec:
      containers:
        - name: saars-console
          image: saars/netcore-console:1.0.1
          env:
          - name: AI_KEY
            value: 803abd4d-b27d-43f7-xxxx-whateverinyourkey

Remember to set the environment variable of AI_KEY as needed.

Please give it a try and feel free to open issues for any questions!