@@ -171,6 +171,7 @@ internal static string GetLogDirectory(IConfigurationTelemetry telemetry)
171171 private static string GetLogDirectory ( IConfigurationSource source , IConfigurationTelemetry telemetry )
172172 {
173173 var logDirectory = new ConfigurationBuilder ( source , telemetry ) . WithKeys ( ConfigurationKeys . LogDirectory ) . AsString ( ) ;
174+
174175 if ( string . IsNullOrEmpty ( logDirectory ) )
175176 {
176177#pragma warning disable 618 // ProfilerLogPath is deprecated but still supported
@@ -183,55 +184,62 @@ private static string GetLogDirectory(IConfigurationSource source, IConfiguratio
183184 }
184185 }
185186
186- return GetDefaultLogDirectory ( source , telemetry , logDirectory ) ;
187+ if ( string . IsNullOrEmpty ( logDirectory ) )
188+ {
189+ logDirectory = GetDefaultLogDirectory ( source , telemetry ) ;
190+ }
191+
192+ if ( Directory . Exists ( logDirectory ) || TryCreateLogDirectory ( logDirectory ) )
193+ {
194+ return logDirectory ;
195+ }
196+
197+ // Last effort at writing logs
198+ return Path . GetTempPath ( ) ;
187199 }
188200
189- private static string GetDefaultLogDirectory ( IConfigurationSource source , IConfigurationTelemetry telemetry , string ? logDirectory )
201+ private static string GetDefaultLogDirectory ( IConfigurationSource source , IConfigurationTelemetry telemetry )
190202 {
191203 // This entire block may throw a SecurityException if not granted the System.Security.Permissions.FileIOPermission
192204 // because of the following API calls
193205 // - Directory.Exists
206+ // - Directory.CreateDirectory
194207 // - Environment.GetFolderPath
195208 // - Path.GetTempPath
196- if ( string . IsNullOrEmpty ( logDirectory ) )
197- {
198- #if NETFRAMEWORK
199- logDirectory = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . CommonApplicationData ) , @"Datadog .NET Tracer" , "logs" ) ;
200- #else
201- var isWindows = FrameworkDescription . Instance . IsWindows ( ) ;
209+ string logDirectory ;
210+ var isWindows = FrameworkDescription . Instance . IsWindows ( ) ;
202211
203- if ( ImmutableAzureAppServiceSettings . GetIsAzureAppService ( source , telemetry ) )
204- {
205- return isWindows ? @"C:\home\LogFiles\datadog" : "/home/LogFiles/datadog" ;
206- }
207- else if ( isWindows )
208- {
209- logDirectory = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . CommonApplicationData ) , @"Datadog .NET Tracer" , "logs" ) ;
210- }
211- else
212- {
213- // Linux or GCP Functions
214- logDirectory = "/var/log/datadog/dotnet" ;
215- }
216- #endif
212+ if ( ImmutableAzureAppServiceSettings . GetIsAzureAppService ( source , telemetry ) )
213+ {
214+ return isWindows ? @"C:\home\LogFiles\datadog" : "/home/LogFiles/datadog" ;
217215 }
218216
219- if ( ! Directory . Exists ( logDirectory ) )
217+ if ( isWindows )
220218 {
221- try
222- {
223- Directory . CreateDirectory ( logDirectory ) ;
224- }
225- catch
226- {
227- // Unable to create the directory meaning that the user
228- // will have to create it on their own.
229- // Last effort at writing logs
230- logDirectory = Path . GetTempPath ( ) ;
231- }
219+ var commonApplicationDataFolder = Environment . GetFolderPath ( Environment . SpecialFolder . CommonApplicationData ) ;
220+ logDirectory = Path . Combine ( commonApplicationDataFolder , "Datadog .NET Tracer" , "logs" ) ;
232221 }
222+ else
223+ {
224+ logDirectory = "/var/log/datadog/dotnet" ;
225+ }
226+
227+ return logDirectory ;
228+ }
233229
234- return logDirectory ! ;
230+ private static bool TryCreateLogDirectory ( string logDirectory )
231+ {
232+ try
233+ {
234+ Directory . CreateDirectory ( logDirectory ) ;
235+ return true ;
236+ }
237+ catch
238+ {
239+ // Unable to create the directory meaning that the user
240+ // will have to create it on their own.
241+ return false ;
242+ }
235243 }
236244
237245 private static FileLoggingConfiguration ? GetFileLoggingConfiguration ( IConfigurationSource source , IConfigurationTelemetry telemetry )
0 commit comments