@@ -45,33 +45,43 @@ namespace Microsoft.Extensions.DependencyInjection
45
45
{
46
46
public static class ServiceCollectionExtensions
47
47
{
48
- public static IServiceCollection AddSkyAPM ( this IServiceCollection services , Action < SkyApmExtensions > extensionsSetup = null )
48
+ public static IServiceCollection AddSkyAPM ( this IServiceCollection services ,
49
+ Action < SkyApmExtensions > extensionsSetup = null )
49
50
{
51
+ #region can be optimized
52
+
50
53
string environment = Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ;
51
54
if ( environment == null || environment . Length < 1 )
52
55
{
53
56
environment = "Development" ;
54
57
}
58
+
55
59
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder ( ) ;
56
60
configurationBuilder . AddJsonFile ( "skyapm.json" , true ) ;
57
61
configurationBuilder . AddJsonFile ( "skyapm." + environment + ".json" , true ) ;
58
62
IConfiguration configuration = configurationBuilder . Build ( ) ;
59
- string enable = configuration ? . GetSection ( "SkyWalking:Enable" ) . Value ?? "false" ;
60
- if ( null == enable || ! "true ". Equals ( enable . ToLower ( ) ) )
63
+ string enable = configuration ? . GetSection ( "SkyWalking:Enable" ) . Value ;
64
+ if ( enable != null && "false ". Equals ( enable . ToLower ( ) ) )
61
65
{
62
66
return services ;
63
67
}
68
+
64
69
string serviceName = configuration ? . GetSection ( "SkyWalking:ServiceName" ) . Value ?? "" ;
65
70
if ( null == serviceName || serviceName . Length < 1 )
66
71
{
67
72
return services ;
68
73
}
74
+
69
75
Environment . SetEnvironmentVariable ( "SKYWALKING__SERVICENAME" , serviceName ) ;
76
+
77
+ #endregion
78
+
70
79
services . AddSkyAPMCore ( extensionsSetup ) ;
71
80
return services ;
72
81
}
73
82
74
- private static IServiceCollection AddSkyAPMCore ( this IServiceCollection services , Action < SkyApmExtensions > extensionsSetup = null )
83
+ private static IServiceCollection AddSkyAPMCore ( this IServiceCollection services ,
84
+ Action < SkyApmExtensions > extensionsSetup = null )
75
85
{
76
86
if ( services == null )
77
87
{
@@ -125,7 +135,7 @@ private static IServiceCollection AddTracing(this IServiceCollection services)
125
135
services . AddSingleton < IBase64Formatter , Base64Formatter > ( ) ;
126
136
return services ;
127
137
}
128
-
138
+
129
139
private static IServiceCollection AddSampling ( this IServiceCollection services )
130
140
{
131
141
services . AddSingleton < SimpleCountSamplingInterceptor > ( ) ;
@@ -153,6 +163,5 @@ private static IServiceCollection AddSkyApmLogging(this IServiceCollection servi
153
163
services . AddSingleton < ILoggerFactory , DefaultLoggerFactory > ( ) ;
154
164
return services ;
155
165
}
156
-
157
166
}
158
167
}
0 commit comments