|
| 1 | +--- |
| 2 | +title: Deploy a .NET Application |
| 3 | +linkTitle: 3. Deploy a .NET Application |
| 4 | +weight: 3 |
| 5 | +time: 10 minutes |
| 6 | +--- |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +Before deploying the application, we'll need to install the .NET 8 SDK on our instance. |
| 11 | + |
| 12 | +{{< tabs >}} |
| 13 | +{{% tab title="Script" %}} |
| 14 | + |
| 15 | +``` bash |
| 16 | +sudo apt-get update && \ |
| 17 | + sudo apt-get install -y dotnet-sdk-8.0 |
| 18 | +``` |
| 19 | + |
| 20 | +{{% /tab %}} |
| 21 | +{{% tab title="Example Output" %}} |
| 22 | + |
| 23 | +``` bash |
| 24 | +Hit:1 http://us-west-1.ec2.archive.ubuntu.com/ubuntu jammy InRelease |
| 25 | +Hit:2 http://us-west-1.ec2.archive.ubuntu.com/ubuntu jammy-updates InRelease |
| 26 | +Hit:3 http://us-west-1.ec2.archive.ubuntu.com/ubuntu jammy-backports InRelease |
| 27 | +Hit:4 http://security.ubuntu.com/ubuntu jammy-security InRelease |
| 28 | +Ign:5 https://splunk.jfrog.io/splunk/otel-collector-deb release InRelease |
| 29 | +Hit:6 https://splunk.jfrog.io/splunk/otel-collector-deb release Release |
| 30 | +Reading package lists... Done |
| 31 | +Reading package lists... Done |
| 32 | +Building dependency tree... Done |
| 33 | +Reading state information... Done |
| 34 | +The following additional packages will be installed: |
| 35 | + aspnetcore-runtime-8.0 aspnetcore-targeting-pack-8.0 dotnet-apphost-pack-8.0 dotnet-host-8.0 dotnet-hostfxr-8.0 dotnet-runtime-8.0 dotnet-targeting-pack-8.0 dotnet-templates-8.0 liblttng-ust-common1 |
| 36 | + liblttng-ust-ctl5 liblttng-ust1 netstandard-targeting-pack-2.1-8.0 |
| 37 | +The following NEW packages will be installed: |
| 38 | + aspnetcore-runtime-8.0 aspnetcore-targeting-pack-8.0 dotnet-apphost-pack-8.0 dotnet-host-8.0 dotnet-hostfxr-8.0 dotnet-runtime-8.0 dotnet-sdk-8.0 dotnet-targeting-pack-8.0 dotnet-templates-8.0 |
| 39 | + liblttng-ust-common1 liblttng-ust-ctl5 liblttng-ust1 netstandard-targeting-pack-2.1-8.0 |
| 40 | +0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded. |
| 41 | +Need to get 138 MB of archives. |
| 42 | +After this operation, 495 MB of additional disk space will be used. |
| 43 | +etc. |
| 44 | +``` |
| 45 | + |
| 46 | +{{% /tab %}} |
| 47 | +{{< /tabs >}} |
| 48 | + |
| 49 | +Refer to [Install .NET SDK or .NET Runtime on Ubuntu](https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu-install?tabs=dotnet8&pivots=os-linux-ubuntu-2404) |
| 50 | +for further details. |
| 51 | + |
| 52 | +## Review the .NET Application |
| 53 | + |
| 54 | +In the terminal, navigate to the application directory: |
| 55 | + |
| 56 | +``` bash |
| 57 | +cd ~/workshop/docker-k8s-otel/helloworld |
| 58 | +``` |
| 59 | + |
| 60 | +We'll use a simple "Hello World" .NET application for this workshop. The main logic is found |
| 61 | +in the Program.cs file: |
| 62 | + |
| 63 | +``` cs |
| 64 | +using System.Globalization; |
| 65 | + |
| 66 | +using Microsoft.AspNetCore.Mvc; |
| 67 | + |
| 68 | +var builder = WebApplication.CreateBuilder(args); |
| 69 | +var app = builder.Build(); |
| 70 | + |
| 71 | +string Hello([FromServices]ILogger<Program> logger, string? name) |
| 72 | +{ |
| 73 | + if (string.IsNullOrEmpty(name)) |
| 74 | + { |
| 75 | + logger.LogInformation("Hello, World!); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + logger.LogInformation("Hello, {result}!", name); |
| 80 | + } |
| 81 | + |
| 82 | + return result.ToString(CultureInfo.InvariantCulture); |
| 83 | +} |
| 84 | + |
| 85 | +app.MapGet("/hello/{name?}", Hello); |
| 86 | + |
| 87 | +app.Run(); |
| 88 | +``` |
| 89 | + |
| 90 | +## Build and Run the .NET Application |
| 91 | + |
| 92 | +We can build the application using the following command: |
| 93 | + |
| 94 | +{{< tabs >}} |
| 95 | +{{% tab title="Script" %}} |
| 96 | + |
| 97 | +``` bash |
| 98 | +dotnet build |
| 99 | +``` |
| 100 | + |
| 101 | +{{% /tab %}} |
| 102 | +{{% tab title="Example Output" %}} |
| 103 | + |
| 104 | +``` bash |
| 105 | +MSBuild version 17.8.5+b5265ef37 for .NET |
| 106 | + Determining projects to restore... |
| 107 | + All projects are up-to-date for restore. |
| 108 | + helloworld -> /home/splunk/workshop/docker-k8s-otel/helloworld/bin/Debug/net8.0/helloworld.dll |
| 109 | + |
| 110 | +Build succeeded. |
| 111 | + 0 Warning(s) |
| 112 | + 0 Error(s) |
| 113 | + |
| 114 | +Time Elapsed 00:00:02.04 |
| 115 | +``` |
| 116 | + |
| 117 | +{{% /tab %}} |
| 118 | +{{< /tabs >}} |
| 119 | + |
| 120 | +If that's successful, we can run it as follows: |
| 121 | + |
| 122 | +{{< tabs >}} |
| 123 | +{{% tab title="Script" %}} |
| 124 | + |
| 125 | +``` bash |
| 126 | +dotnet run |
| 127 | +``` |
| 128 | + |
| 129 | +{{% /tab %}} |
| 130 | +{{% tab title="Example Output" %}} |
| 131 | + |
| 132 | +``` bash |
| 133 | +Building... |
| 134 | +info: Microsoft.Hosting.Lifetime[14] |
| 135 | + Now listening on: http://localhost:8080 |
| 136 | +info: Microsoft.Hosting.Lifetime[0] |
| 137 | + Application started. Press Ctrl+C to shut down. |
| 138 | +info: Microsoft.Hosting.Lifetime[0] |
| 139 | + Hosting environment: Development |
| 140 | +info: Microsoft.Hosting.Lifetime[0] |
| 141 | + Content root path: /home/splunk/workshop/docker-k8s-otel/helloworld |
| 142 | +``` |
| 143 | + |
| 144 | +{{% /tab %}} |
| 145 | +{{< /tabs >}} |
| 146 | + |
| 147 | +Once it's running, open a second SSH terminal and access the application using curl: |
| 148 | + |
| 149 | +{{< tabs >}} |
| 150 | +{{% tab title="Script" %}} |
| 151 | + |
| 152 | +``` bash |
| 153 | +curl http://localhost:8080/hello |
| 154 | +``` |
| 155 | + |
| 156 | +{{% /tab %}} |
| 157 | +{{% tab title="Example Output" %}} |
| 158 | + |
| 159 | +``` bash |
| 160 | +Hello, World! |
| 161 | +``` |
| 162 | + |
| 163 | +{{% /tab %}} |
| 164 | +{{< /tabs >}} |
| 165 | + |
| 166 | +You can also pass in your name: |
| 167 | + |
| 168 | +{{< tabs >}} |
| 169 | +{{% tab title="Script" %}} |
| 170 | + |
| 171 | +``` bash |
| 172 | +curl http://localhost:8080/hello/Tom |
| 173 | +``` |
| 174 | + |
| 175 | +{{% /tab %}} |
| 176 | +{{% tab title="Example Output" %}} |
| 177 | + |
| 178 | +``` bash |
| 179 | +Hello, Tom! |
| 180 | +``` |
| 181 | + |
| 182 | +{{% /tab %}} |
| 183 | +{{< /tabs >}} |
| 184 | + |
| 185 | +## Next Steps |
| 186 | + |
| 187 | +What are the two methods that we can use to instrument our application with OpenTelemetry? |
| 188 | + |
| 189 | +Hint: see [Instrument your .NET application for Splunk Observability Cloud](https://docs.splunk.com/observability/en/gdi/get-data-in/application/otel-dotnet/instrumentation/instrument-dotnet-application.html) |
| 190 | +for a discussion of the options. |
0 commit comments