11// Copyright The OpenTelemetry Authors
22// SPDX-License-Identifier: Apache-2.0
33
4+ using System . Diagnostics ;
45using Microsoft . Extensions . Logging ;
56using OpenTelemetry ;
67using 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 ;
0 commit comments