88
99namespace OpenTelemetry . Internal ;
1010
11- internal sealed class SelfDiagnosticsConfigParser
11+ /// <summary>
12+ /// Parses the self-diagnostics configuration file.
13+ /// </summary>
14+ internal sealed partial class SelfDiagnosticsConfigParser
1215{
1316 public const string ConfigFileName = "OTEL_DIAGNOSTICS.json" ;
1417 private const int FileSizeLowerLimit = 1024 ; // Lower limit for log file size in KB: 1MB
@@ -19,17 +22,20 @@ internal sealed class SelfDiagnosticsConfigParser
1922 /// </summary>
2023 private const int ConfigBufferSize = 4 * 1024 ;
2124
22- private static readonly Regex LogDirectoryRegex = new (
23- @"""LogDirectory""\s*:\s*""(?<LogDirectory>.*?)""" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
25+ private const string LogDirectoryRegexPattern = @"""LogDirectory""\s*:\s*""(?<LogDirectory>.*?)""" ;
26+ private const string FileSizeRegexPattern = @"""FileSize""\s*:\s*(?<FileSize>\d+)" ;
27+ private const string LogLevelRegexPattern = @"""LogLevel""\s*:\s*""(?<LogLevel>.*?)""" ;
28+ private const string FormatMessageRegexPattern = @"""FormatMessage""\s*:\s*(?:""(?<FormatMessage>.*?)""|(?<FormatMessage>true|false))" ;
2429
25- private static readonly Regex FileSizeRegex = new (
26- @"""FileSize""\s*:\s*(?<FileSize>\d+)" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
30+ #if ! NET
31+ private static readonly Regex LogDirectoryRegex = new ( LogDirectoryRegexPattern , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
2732
28- private static readonly Regex LogLevelRegex = new (
29- @"""LogLevel""\s*:\s*""(?<LogLevel>.*?)""" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
33+ private static readonly Regex FileSizeRegex = new ( FileSizeRegexPattern , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
3034
31- private static readonly Regex FormatMessageRegex = new (
32- @"""FormatMessage""\s*:\s*(?:""(?<FormatMessage>.*?)""|(?<FormatMessage>true|false))" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
35+ private static readonly Regex LogLevelRegex = new ( LogLevelRegexPattern , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
36+
37+ private static readonly Regex FormatMessageRegex = new ( FormatMessageRegexPattern , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
38+ #endif
3339
3440 // This class is called in SelfDiagnosticsConfigRefresher.UpdateMemoryMappedFileFromConfiguration
3541 // in both main thread and the worker thread.
@@ -128,15 +134,15 @@ internal static bool TryParseLogDirectory(
128134 string configJson ,
129135 out string logDirectory )
130136 {
131- var logDirectoryResult = LogDirectoryRegex . Match ( configJson ) ;
137+ var logDirectoryResult = GetLogDirectoryRegex ( ) . Match ( configJson ) ;
132138 logDirectory = logDirectoryResult . Groups [ "LogDirectory" ] . Value ;
133139 return logDirectoryResult . Success && ! string . IsNullOrWhiteSpace ( logDirectory ) ;
134140 }
135141
136142 internal static bool TryParseFileSize ( string configJson , out int fileSizeInKB )
137143 {
138144 fileSizeInKB = 0 ;
139- var fileSizeResult = FileSizeRegex . Match ( configJson ) ;
145+ var fileSizeResult = GetFileSizeRegex ( ) . Match ( configJson ) ;
140146 return fileSizeResult . Success && int . TryParse ( fileSizeResult . Groups [ "FileSize" ] . Value , out fileSizeInKB ) ;
141147 }
142148
@@ -145,15 +151,15 @@ internal static bool TryParseLogLevel(
145151 [ NotNullWhen ( true ) ]
146152 out string ? logLevel )
147153 {
148- var logLevelResult = LogLevelRegex . Match ( configJson ) ;
154+ var logLevelResult = GetLogLevelRegex ( ) . Match ( configJson ) ;
149155 logLevel = logLevelResult . Groups [ "LogLevel" ] . Value ;
150156 return logLevelResult . Success && ! string . IsNullOrWhiteSpace ( logLevel ) ;
151157 }
152158
153159 internal static bool TryParseFormatMessage ( string configJson , out bool formatMessage )
154160 {
155161 formatMessage = false ;
156- var formatMessageResult = FormatMessageRegex . Match ( configJson ) ;
162+ var formatMessageResult = GetFormatMessageRegex ( ) . Match ( configJson ) ;
157163 if ( formatMessageResult . Success )
158164 {
159165 var formatMessageValue = formatMessageResult . Groups [ "FormatMessage" ] . Value ;
@@ -162,4 +168,26 @@ internal static bool TryParseFormatMessage(string configJson, out bool formatMes
162168
163169 return true ;
164170 }
171+
172+ #if NET
173+ [ GeneratedRegex ( LogDirectoryRegexPattern , RegexOptions . IgnoreCase ) ]
174+ private static partial Regex GetLogDirectoryRegex ( ) ;
175+
176+ [ GeneratedRegex ( FileSizeRegexPattern , RegexOptions . IgnoreCase ) ]
177+ private static partial Regex GetFileSizeRegex ( ) ;
178+
179+ [ GeneratedRegex ( LogLevelRegexPattern , RegexOptions . IgnoreCase ) ]
180+ private static partial Regex GetLogLevelRegex ( ) ;
181+
182+ [ GeneratedRegex ( FormatMessageRegexPattern , RegexOptions . IgnoreCase ) ]
183+ private static partial Regex GetFormatMessageRegex ( ) ;
184+ #else
185+ private static Regex GetLogDirectoryRegex ( ) => LogDirectoryRegex ;
186+
187+ private static Regex GetFileSizeRegex ( ) => FileSizeRegex ;
188+
189+ private static Regex GetLogLevelRegex ( ) => LogLevelRegex ;
190+
191+ private static Regex GetFormatMessageRegex ( ) => FormatMessageRegex ;
192+ #endif
165193}
0 commit comments