14
14
// limitations under the License.
15
15
// </copyright>
16
16
17
+ using System ;
17
18
using Microsoft . AspNetCore . Hosting ;
18
19
using Microsoft . Extensions . Configuration ;
20
+ using Microsoft . Extensions . DependencyInjection ;
19
21
using Microsoft . Extensions . Hosting ;
20
22
using Microsoft . Extensions . Logging ;
21
23
using OpenTelemetry . Logs ;
24
+ using OpenTelemetry . Resources ;
22
25
23
26
namespace Examples . AspNetCore
24
27
{
@@ -40,17 +43,38 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
40
43
builder . ClearProviders ( ) ;
41
44
builder . AddConsole ( ) ;
42
45
43
- var useLogging = context . Configuration . GetValue < bool > ( "UseLogging" ) ;
44
- if ( useLogging )
46
+ var logExporter = context . Configuration . GetValue < string > ( "UseLogExporter" ) . ToLowerInvariant ( ) ;
47
+ switch ( logExporter )
45
48
{
46
- builder . AddOpenTelemetry ( options =>
47
- {
48
- options . IncludeScopes = true ;
49
- options . ParseStateValues = true ;
50
- options . IncludeFormattedMessage = true ;
51
- options . AddConsoleExporter ( ) ;
52
- } ) ;
49
+ case "otlp" :
50
+ // Adding the OtlpExporter creates a GrpcChannel.
51
+ // This switch must be set before creating a GrpcChannel when calling an insecure gRPC service.
52
+ // See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
53
+ AppContext . SetSwitch ( "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport" , true ) ;
54
+ builder . AddOpenTelemetry ( options =>
55
+ {
56
+ options . SetResourceBuilder ( ResourceBuilder . CreateDefault ( ) . AddService ( context . Configuration . GetValue < string > ( "Otlp:ServiceName" ) ) ) ;
57
+ options . AddOtlpExporter ( otlpOptions =>
58
+ {
59
+ otlpOptions . Endpoint = new Uri ( context . Configuration . GetValue < string > ( "Otlp:Endpoint" ) ) ;
60
+ } ) ;
61
+ } ) ;
62
+ break ;
63
+
64
+ default :
65
+ builder . AddOpenTelemetry ( options =>
66
+ {
67
+ options . AddConsoleExporter ( ) ;
68
+ } ) ;
69
+ break ;
53
70
}
71
+
72
+ builder . Services . Configure < OpenTelemetryLoggerOptions > ( opt =>
73
+ {
74
+ opt . IncludeScopes = true ;
75
+ opt . ParseStateValues = true ;
76
+ opt . IncludeFormattedMessage = true ;
77
+ } ) ;
54
78
} ) ;
55
79
}
56
80
}
0 commit comments