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

Add ASP.NET Core logging example #4821

Merged
merged 26 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0f06e8f
Add ASP.NET Core logging example
reyang Aug 7, 2023
163b3ab
complete example
reyang Sep 1, 2023
6c08d8d
add more explanation
reyang Sep 1, 2023
3b38af8
reword
reyang Sep 1, 2023
f843084
Merge branch 'main' into reyang/aspnetcore-logging-example
reyang Sep 5, 2023
ff1b1e2
fix link
reyang Sep 5, 2023
2d0c156
update based on review discussions
reyang Sep 6, 2023
1284dc0
Merge branch 'main' into reyang/aspnetcore-logging-example
reyang Sep 6, 2023
5a469a8
cleanup notes
reyang Sep 6, 2023
ee8ef0c
minor fixes
reyang Sep 6, 2023
709fa0e
update output
reyang Sep 6, 2023
b907760
Merge branch 'main' into reyang/aspnetcore-logging-example
reyang Sep 7, 2023
0c20b6a
update the docs
reyang Sep 7, 2023
7fd76a8
wording fix
reyang Sep 7, 2023
6d99545
wording fix
reyang Sep 7, 2023
dcb62f1
Merge branch 'main' into reyang/aspnetcore-logging-example
reyang Sep 8, 2023
f2df0a6
remove default logging providers and simplify the console output
reyang Sep 8, 2023
b67ca88
Merge branch 'main' into reyang/aspnetcore-logging-example
reyang Sep 11, 2023
1dcc8f6
Update docs/logs/getting-started-aspnetcore/README.md
reyang Sep 11, 2023
7763463
format
reyang Sep 11, 2023
db5648d
reword
reyang Sep 11, 2023
0184cc0
reword
reyang Sep 11, 2023
061748d
clarified that console exporter is not supposed to be used in product…
reyang Sep 14, 2023
96da4af
Merge branch 'main' into reyang/aspnetcore-logging-example
reyang Sep 14, 2023
c491e52
Update docs/logs/getting-started-aspnetcore/Program.cs
reyang Sep 15, 2023
f95395f
Merge branch 'main' into reyang/aspnetcore-logging-example
utpilla Sep 15, 2023
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
24 changes: 24 additions & 0 deletions docs/logs/getting-started-aspnetcore/HelloLogs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <copyright file="HelloLogs.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

public static partial class HelloLogs
{
[LoggerMessage(
EventId = 1,
Level = LogLevel.Information,
Message = "Hello, world!")]
reyang marked this conversation as resolved.
Show resolved Hide resolved
public static partial void SayHello(this ILogger logger);
}
44 changes: 44 additions & 0 deletions docs/logs/getting-started-aspnetcore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <copyright file="Program.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using OpenTelemetry.Logs;
using OpenTelemetry.Resources;

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeScopes = true;
logging.ParseStateValues = true;
reyang marked this conversation as resolved.
Show resolved Hide resolved

var resourceBuilder = ResourceBuilder
.CreateDefault()
.AddService(builder.Environment.ApplicationName);

logging.SetResourceBuilder(resourceBuilder)
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
.AddConsoleExporter();
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
reyang marked this conversation as resolved.
Show resolved Hide resolved
});

var app = builder.Build();

app.MapGet("/", (ILogger<Program> logger) =>
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
logger.SayHello();

return "Hello from OpenTelemetry Logs!";
});

app.Run();
73 changes: 73 additions & 0 deletions docs/logs/getting-started-aspnetcore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Getting Started with OpenTelemetry .NET Logs in 5 Minutes - ASP.NET Core Application

First, download and install the [.NET
SDK](https://dotnet.microsoft.com/download) on your computer.

Create a new web application:

```sh
dotnet new web -o aspnetcoreapp
cd aspnetcoreapp
```

Install the
[OpenTelemetry.Exporter.Console](../../../src/OpenTelemetry.Exporter.Console/README.md)
and
[OpenTelemetry.Extensions.Hosting](../../../src/OpenTelemetry.Extensions.Hosting/README.md)
packages:

```sh
dotnet add package OpenTelemetry.Exporter.Console
dotnet add package OpenTelemetry.Extensions.Hosting
```

Copy the [HelloLogs.cs](./HelloLogs.cs) and [Program.cs](./Program.cs) files to
reyang marked this conversation as resolved.
Show resolved Hide resolved
the project folder.

Run the application again (using `dotnet run`) and then browse to the URL shown
in the console for your application (e.g. `http://localhost:5154`). You should see
the logs output from the console.

```text
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
LogRecord.Timestamp: 2023-09-01T22:55:50.6389757Z
LogRecord.TraceId: 8fa0ec5519c9bd5a498978c089a78182
LogRecord.SpanId: b888feebe74e9685
LogRecord.TraceFlags: None
LogRecord.CategoryName: Program
LogRecord.Severity: Info
LogRecord.SeverityText: Information
LogRecord.Body: Hello, world!
LogRecord.Attributes (Key:Value):
OriginalFormat (a.k.a Body): Hello, world!
LogRecord.EventId: 1
LogRecord.EventName: SayHello
LogRecord.ScopeValues (Key:Value):
[Scope.0]:SpanId: b888feebe74e9685
reyang marked this conversation as resolved.
Show resolved Hide resolved
[Scope.0]:TraceId: 8fa0ec5519c9bd5a498978c089a78182
[Scope.0]:ParentId: 0000000000000000
[Scope.1]:ConnectionId: 0HMTB7Q8V1EHB
[Scope.2]:RequestId: 0HMTB7Q8V1EHB:0000000F
[Scope.2]:RequestPath: /

Resource associated with LogRecord:
service.name: getting-started-aspnetcore
service.instance.id: 144fa25b-b25c-4829-b8f6-d465df52fdaa
telemetry.sdk.name: opentelemetry
telemetry.sdk.language: dotnet
telemetry.sdk.version: 1.6.0-rc.1.15
```

Congratulations! You are now collecting logs using OpenTelemetry.

What does the above program do?

The program has configured the ASP.NET Core logging pipeline by enabling
reyang marked this conversation as resolved.
Show resolved Hide resolved
OpenTelemetry SDK and the `ConsoleExporter`. `ConsoleExporter` simply displays
it on the console.

## Learn more

* [Compile-time logging source
generation](https://docs.microsoft.com/dotnet/core/extensions/logger-message-generator)
* [Customizing the OpenTelemetry .NET SDK](../customizing-the-sdk/README.md)
* [Extending the OpenTelemetry .NET SDK](../extending-the-sdk/README.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"DetailedErrors": true,
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions docs/logs/getting-started-aspnetcore/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
utpilla marked this conversation as resolved.
Show resolved Hide resolved
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" />
</ItemGroup>

</Project>