Skip to content

Commit

Permalink
Merge branch 'master' into rca-ftw-aug20
Browse files Browse the repository at this point in the history
  • Loading branch information
Simplychee authored Aug 26, 2024
2 parents 5afa78a + fe319d2 commit 4ef12a8
Show file tree
Hide file tree
Showing 11 changed files with 623 additions and 156 deletions.
122 changes: 64 additions & 58 deletions docs/shipping/Code/dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -1197,14 +1197,14 @@ The following example uses a basic [Minimal API with ASP.NET Core](https://learn

### Create and launch an HTTP Server

To begin, set up an environment in a new directory called `dotnet-simple`. Within that directory, execute following command:
1. Set up an environment in a new directory called `dotnet-simple`. Within that directory, execute following command:

```
```bash
dotnet new web
```
In the same directory, replace the content of Program.cs with the following code:
2. In the same directory, replace the content of `Program.cs` with the following code:

```
```csharp
using System.Globalization;

using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -1239,7 +1239,7 @@ app.Run();

```

In the Properties subdirectory, replace the content of launchSettings.json with the following:
3. In the `Properties` subdirectory, replace the content of `launchSettings.json` with the following:

```
{
Expand All @@ -1259,75 +1259,81 @@ In the Properties subdirectory, replace the content of launchSettings.json with
```

Build and run the application with the following command, then open http://localhost:8080/rolldice in your web browser to ensure it is working.
4. Build and run the application with the following command, then open http://localhost:8080/rolldice in your web browser to ensure it is working.

```
dotnet build
dotnet run
```
### Instrumentation

Next we’ll install the instrumentation [NuGet packages from OpenTelemetry](https://www.nuget.org/profiles/OpenTelemetry) that will generate the telemetry, and set them up.
Next, we'll configure the OpenTelemetry logging exporter to send logs to Logz.io via the OTLP listener.

This configuration is designed to send logs to your Logz.io account via the OpenTelemetry Protocol (OTLP) listener. You need to specify your Logz.io token and configure the listener endpoint to match the correct region. By default, the endpoint is `https://otlp-listener.logz.io/v1/logs`, but it should be adjusted based on your region. You can find more details on the regional configurations in the [Hosting Regions Documentation](https://docs.logz.io/docs/user-guide/admin/hosting-regions/account-region/#available-regions).



1. Add the packages
```
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
```
2. Setup the OpenTelemetry code
In Program.cs, replace the following lines:
```
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
```
With:
```
using OpenTelemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using OpenTelemetry.Exporter;
var builder = WebApplication.CreateBuilder(args);
const string serviceName = "roll-dice";
const string logzioEndpoint = "https://otlp-listener.logz.io/v1/logs";
const string logzioToken = "<LOG-SHIPPING-TOKEN>";
builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(logzioEndpoint);
otlpOptions.Headers = $"Authorization=Bearer {logzioToken}, user-agent=logzio-dotnet-logs";
otlpOptions.Protocol = OtlpExportProtocol.HttpProtobuf;
});
});
var app = builder.Build();
```

```bash
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
```

2. Setup the OpenTelemetry code in `Program.cs`, by replacing the following lines:

```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
```

With:


```csharp
using OpenTelemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using OpenTelemetry.Exporter;
var builder = WebApplication.CreateBuilder(args);
const string serviceName = "roll-dice";
const string logzioEndpoint = "https://otlp-listener.logz.io/v1/logs";
const string logzioToken = "<LOG-SHIPPING-TOKEN>";
builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(logzioEndpoint);
otlpOptions.Headers = $"Authorization=Bearer {logzioToken}, user-agent=logzio-dotnet-logs";
otlpOptions.Protocol = OtlpExportProtocol.HttpProtobuf;
});
});
var app = builder.Build();
```

{@include: ../../_include/log-shipping/log-shipping-token.md}
Update the `listener.logz.io` parth in `https://otlp-listener.logz.io/v1/logs` with the URL for [your hosting region](https://docs.logz.io/docs/user-guide/admin/hosting-regions/account-region).

3. Run your **application** once again:

```
dotnet run
```
Note the output from the dotnet run.
```bash
dotnet run
```

4. From another terminal, send a request using curl:

```
curl localhost:8080/rolldice
```
```bash
curl localhost:8080/rolldice
```

5. After about 30 sec, stop the server process.

At this point, you should see log output from the server and client on your Logzio account.
At this point, you should see log output from the server and client on your Logz.io account.


</TabItem>
Expand Down
Loading

0 comments on commit 4ef12a8

Please sign in to comment.