-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs-logging] Add a logging doc for showing how to set up a dedicate…
…d pipeline for specific logs (#5356)
- Loading branch information
1 parent
855e9c4
commit e4b08ac
Showing
8 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
docs/logs/dedicated-pipeline/DedicatedLogging/DedicatedLoggingServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using OpenTelemetry.Logs; | ||
|
||
namespace DedicatedLogging; | ||
|
||
public static class DedicatedLoggingServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddDedicatedLogging( | ||
this IServiceCollection services, | ||
IConfiguration configuration, | ||
Action<OpenTelemetryLoggerOptions> configureOpenTelemetry) | ||
{ | ||
ArgumentNullException.ThrowIfNull(configureOpenTelemetry); | ||
|
||
services.TryAddSingleton(sp => | ||
{ | ||
var loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder.AddConfiguration(configuration); | ||
|
||
builder.AddOpenTelemetry(configureOpenTelemetry); | ||
}); | ||
|
||
return new DedicatedLoggerFactory(loggerFactory); | ||
}); | ||
|
||
services.TryAdd(ServiceDescriptor.Singleton(typeof(IDedicatedLogger<>), typeof(DedicatedLogger<>))); | ||
|
||
return services; | ||
} | ||
|
||
private sealed class DedicatedLogger<T> : IDedicatedLogger<T> | ||
{ | ||
private readonly ILogger innerLogger; | ||
|
||
public DedicatedLogger(DedicatedLoggerFactory loggerFactory) | ||
{ | ||
this.innerLogger = loggerFactory.CreateLogger(typeof(T).FullName!); | ||
} | ||
|
||
public IDisposable? BeginScope<TState>(TState state) | ||
where TState : notnull | ||
=> this.innerLogger.BeginScope(state); | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
=> this.innerLogger.IsEnabled(logLevel); | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
=> this.innerLogger.Log(logLevel, eventId, state, exception, formatter); | ||
} | ||
|
||
private sealed class DedicatedLoggerFactory : ILoggerFactory | ||
{ | ||
private readonly ILoggerFactory innerLoggerFactory; | ||
|
||
public DedicatedLoggerFactory(ILoggerFactory loggerFactory) | ||
{ | ||
this.innerLoggerFactory = loggerFactory; | ||
} | ||
|
||
public void AddProvider(ILoggerProvider provider) | ||
=> this.innerLoggerFactory.AddProvider(provider); | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
=> this.innerLoggerFactory.CreateLogger(categoryName); | ||
|
||
public void Dispose() | ||
=> this.innerLoggerFactory.Dispose(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
docs/logs/dedicated-pipeline/DedicatedLogging/IDedicatedLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
namespace DedicatedLogging; | ||
|
||
public interface IDedicatedLogger : ILogger | ||
{ | ||
} | ||
|
||
public interface IDedicatedLogger<out TCategoryName> : IDedicatedLogger | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using DedicatedLogging; | ||
using OpenTelemetry.Logs; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Logging.ClearProviders(); | ||
|
||
builder.Logging.AddOpenTelemetry(options => | ||
{ | ||
// Set up primary pipeline for common app logs | ||
options.AddConsoleExporter(); | ||
}); | ||
|
||
builder.Services.AddDedicatedLogging( | ||
builder.Configuration.GetSection("DedicatedLogging"), // Bind configuration for dedicated logging pipeline | ||
options => | ||
{ | ||
// Set up secondary pipeline for dedicated logs | ||
options.AddConsoleExporter(); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapGet("/", (HttpContext context, ILogger<Program> logger, IDedicatedLogger<Program> dedicatedLogger) => | ||
{ | ||
// Standard log written | ||
logger.FoodPriceChanged("artichoke", 9.99); | ||
|
||
// Dedicated log written | ||
dedicatedLogger.RequestInitiated(context.Connection.RemoteIpAddress?.ToString() ?? "unknown"); | ||
|
||
return "Hello from OpenTelemetry Logs!"; | ||
}); | ||
|
||
app.Run(); | ||
|
||
internal static partial class LoggerExtensions | ||
{ | ||
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] | ||
public static partial void FoodPriceChanged(this ILogger logger, string name, double price); | ||
|
||
[LoggerMessage(LogLevel.Information, "Request initiated from `{ipAddress}`.")] | ||
public static partial void RequestInitiated(this IDedicatedLogger logger, string ipAddress); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Dedicated pipeline | ||
|
||
This example shows how to create a dedicated logging pipeline for specific logs | ||
which will be sent to a different location than normal application logs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"DetailedErrors": true, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"DedicatedLogging": { | ||
"LogLevel": { | ||
"Default": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<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> |