Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SKD docs #643

Merged
merged 8 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 15 additions & 102 deletions docs/shipping/Code/dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -1185,93 +1185,19 @@ Replace `<<TYPE>>` with the log type to identify these logs in Logz.io.
</TabItem>
<TabItem value="OpenTelemetry" label="OpenTelemetry">


This integration uses the OpenTelemetry logging exporter to send logs to Logz.io via the OpenTelemetry Protocol (OTLP) listener.

### Prerequisites

Ensure that you have the following installed locally:
- [.NET SDK](https://dotnet.microsoft.com/download/dotnet) 6+
- An ASP.NET Core application
- An active account with Logz.io

### Example Application
The following example uses a basic [Minimal API with ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-8.0&tabs=visual-studio) application.

### Create and launch an HTTP Server

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

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

```csharp
using System.Globalization;

using Microsoft.AspNetCore.Mvc;

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

string HandleRollDice([FromServices]ILogger<Program> logger, string? player)
{
var result = RollDice();

if (string.IsNullOrEmpty(player))
{
logger.LogInformation("Anonymous player is rolling the dice: {result}", result);
}
else
{
logger.LogInformation("{player} is rolling the dice: {result}", player, result);
}

return result.ToString(CultureInfo.InvariantCulture);
}

int RollDice()
{
return Random.Shared.Next(1, 7);
}

app.MapGet("/rolldice/{player?}", HandleRollDice);

app.Run();

```

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

```
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

```

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 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).

:::note
If you need an example aplication to test this integration, please refer to our [.NET OpenTelemetry repository](https://github.com/logzio/opentelemetry-examples/tree/main/dotnet/logs).
:::

### Configure the instrumentation

1. Add the packages

Expand All @@ -1281,23 +1207,15 @@ This configuration is designed to send logs to your Logz.io account via the Open
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:

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

```csharp
using OpenTelemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using OpenTelemetry.Exporter;
var builder = WebApplication.CreateBuilder(args);
const string serviceName = "roll-dice";
const string serviceName = "YOUR-SERVICE-NAME";
const string logzioEndpoint = "https://otlp-listener.logz.io/v1/logs";
const string logzioToken = "<LOG-SHIPPING-TOKEN>";
builder.Logging.AddOpenTelemetry(options =>
Expand All @@ -1315,25 +1233,20 @@ This configuration is designed to send logs to your Logz.io account via the Open
});
var app = builder.Build();
```
Replace `YOUR-SERVICE-NAME` with the required service name.

{@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).
nico-shishkin marked this conversation as resolved.
Show resolved Hide resolved

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

```bash
dotnet run
```
### Check Logz.io for your logs

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

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

5. After about 30 sec, stop the server process.
Allow some time for data ingestion, then open [Open Search Dashboards](https://app.logz.io/#/dashboard/osd).

At this point, you should see log output from the server and client on your Logz.io account.
Encounter an issue? See our [log shipping troubleshooting](https://docs.logz.io/docs/user-guide/log-management/troubleshooting/log-shipping-troubleshooting/) guide.


</TabItem>
Expand Down
Loading