Skip to content

Commit 1fdf70a

Browse files
committed
docs(example): update Console example with formatter option (#6391)
1 parent 32dce67 commit 1fdf70a

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

examples/Console/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ internal sealed class MetricsOptions
7676
[Option("useExporter", Default = "console", HelpText = "Options include otlp or console.", Required = false)]
7777
public string? UseExporter { get; set; }
7878

79+
[Option("useFormatter", Default = "compact", HelpText = "Formatter for console exporter include Compact or Detail).", Required = false)]
80+
public string? UseFormatter { get; set; }
81+
7982
[Option('e', "endpoint", HelpText = "Target to which the exporter is going to send metrics (default value depends on protocol).", Default = null)]
8083
public string? Endpoint { get; set; }
8184

@@ -96,6 +99,8 @@ internal sealed class HttpClientOptions
9699
[Verb("console", HelpText = "Specify the options required to test console exporter")]
97100
internal sealed class ConsoleOptions
98101
{
102+
[Option("useFormatter", Default = "compact", HelpText = "Formatter for console exporter include Compact or Detail).", Required = false)]
103+
public string? UseFormatter { get; set; }
99104
}
100105

101106
[Verb("otelshim", HelpText = "Specify the options required to test OpenTelemetry Shim with console exporter")]
@@ -124,6 +129,9 @@ internal sealed class LogsOptions
124129
[Option("useExporter", Default = "otlp", HelpText = "Options include otlp or console.", Required = false)]
125130
public string? UseExporter { get; set; }
126131

132+
[Option("useFormatter", Default = "compact", HelpText = "Formatter for console exporter include Compact or Detail).", Required = false)]
133+
public string? UseFormatter { get; set; }
134+
127135
[Option('e', "endpoint", HelpText = "Target to which the OTLP exporter is going to send logs (default value depends on protocol).", Default = null)]
128136
public string? Endpoint { get; set; }
129137

examples/Console/TestConsoleExporter.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ internal sealed class TestConsoleExporter
1616
//
1717
// dotnet run console
1818
internal static int Run(ConsoleOptions options)
19-
{
20-
return RunWithActivitySource();
21-
}
22-
23-
private static int RunWithActivitySource()
2419
{
2520
// Enable OpenTelemetry for the sources "Samples.SampleServer" and "Samples.SampleClient"
2621
// and use Console exporter.
@@ -30,7 +25,7 @@ private static int RunWithActivitySource()
3025
#pragma warning disable CA2000 // Dispose objects before losing scope
3126
.AddProcessor(new MyProcessor()) // This must be added before ConsoleExporter
3227
#pragma warning restore CA2000 // Dispose objects before losing scope
33-
.AddConsoleExporter()
28+
.AddConsoleExporter(config => config.Formatter = options.UseFormatter ?? "Compact")
3429
.Build();
3530

3631
// The above line is required only in applications

examples/Console/TestLogs.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
using System.Diagnostics;
45
using Microsoft.Extensions.Logging;
56
using OpenTelemetry;
67
using OpenTelemetry.Logs;
@@ -11,8 +12,17 @@ internal sealed class TestLogs
1112
{
1213
internal static int Run(LogsOptions options)
1314
{
15+
// Add ActivitySource listener to enable activity (span) creation
16+
using var listener = new ActivityListener
17+
{
18+
ShouldListenTo = _ => true,
19+
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData,
20+
};
21+
ActivitySource.AddActivityListener(listener);
22+
1423
using var loggerFactory = LoggerFactory.Create(builder =>
1524
{
25+
builder.SetMinimumLevel(LogLevel.Debug);
1626
builder.AddOpenTelemetry((opt) =>
1727
{
1828
opt.IncludeFormattedMessage = true;
@@ -107,16 +117,41 @@ internal static int Run(LogsOptions options)
107117
}
108118
else
109119
{
110-
opt.AddConsoleExporter();
120+
opt.AddConsoleExporter(config => config.Formatter = options.UseFormatter ?? "Compact");
111121
}
112122
});
113123
});
114124

125+
using var activitySource = new ActivitySource("Examples.Console");
115126
var logger = loggerFactory.CreateLogger<TestLogs>();
127+
116128
using (logger.BeginCityScope("Seattle"))
117129
using (logger.BeginStoreTypeScope("Physical"))
118130
{
119131
logger.HelloFrom("tomato", 2.99);
132+
133+
using (var activity = activitySource.StartActivity("TestLogs", ActivityKind.Internal))
134+
{
135+
try
136+
{
137+
#pragma warning disable CA1848 // For example purposes
138+
logger.LogWarning(1234, "Size exceeds {Limit}.", 5);
139+
#pragma warning restore CA1848
140+
141+
using (var activity2 = activitySource.StartActivity("Inner", ActivityKind.Internal))
142+
{
143+
#pragma warning disable CA1848 // For example purposes
144+
logger.LogDebug("Random {Guid}", Guid.NewGuid());
145+
#pragma warning restore CA1848
146+
147+
throw new NotImplementedException("TEST EXCEPTION");
148+
}
149+
}
150+
catch (NotImplementedException ex)
151+
{
152+
logger.CrashMessage("test", ex);
153+
}
154+
}
120155
}
121156

122157
return 0;

examples/Console/TestLogsExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ internal static partial class TestLogsExtensions
1616

1717
[LoggerMessage(LogLevel.Information, "Hello from {Name} {Price}.")]
1818
public static partial void HelloFrom(this ILogger logger, string name, double price);
19+
20+
[LoggerMessage(LogLevel.Critical, "Critical Error from {Source}.")]
21+
public static partial void CrashMessage(this ILogger logger, string source, Exception ex);
1922
}

examples/Console/TestMetrics.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ internal static int Run(MetricsOptions options)
7171
.AddConsoleExporter((exporterOptions, metricReaderOptions) =>
7272
{
7373
exporterOptions.Targets = ConsoleExporterOutputTargets.Console;
74+
exporterOptions.Formatter = options.UseFormatter ?? "Compact";
7475

7576
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = options.DefaultCollectionPeriodMilliseconds;
7677
metricReaderOptions.TemporalityPreference = options.IsDelta ? MetricReaderTemporalityPreference.Delta : MetricReaderTemporalityPreference.Cumulative;

0 commit comments

Comments
 (0)