11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- #nullable disable // TODO: remove and fix errors
5-
64using System ;
75using System . Collections . Concurrent ;
86using System . Collections . Generic ;
@@ -26,7 +24,7 @@ internal class LogsHelper
2624 private static readonly ConcurrentDictionary < int , string > s_depthCache = new ConcurrentDictionary < int , string > ( ) ;
2725 private static readonly Func < int , string > s_convertDepthToStringRef = ConvertDepthToString ;
2826
29- internal static List < TelemetryItem > OtelToAzureMonitorLogs ( Batch < LogRecord > batchLogRecord , AzureMonitorResource resource , string instrumentationKey )
27+ internal static List < TelemetryItem > OtelToAzureMonitorLogs ( Batch < LogRecord > batchLogRecord , AzureMonitorResource ? resource , string instrumentationKey )
3028 {
3129 List < TelemetryItem > telemetryItems = new List < TelemetryItem > ( ) ;
3230 TelemetryItem telemetryItem ;
@@ -57,15 +55,15 @@ internal static List<TelemetryItem> OtelToAzureMonitorLogs(Batch<LogRecord> batc
5755 return telemetryItems ;
5856 }
5957
60- internal static string GetMessageAndSetProperties ( LogRecord logRecord , IDictionary < string , string > properties )
58+ internal static string ? GetMessageAndSetProperties ( LogRecord logRecord , IDictionary < string , string > properties )
6159 {
62- string message = logRecord . FormattedMessage ;
60+ string ? message = logRecord . FormattedMessage ;
6361
6462 // Both logRecord.State and logRecord.StateValues will not be set at the same time for LogRecord.
6563 // Either logRecord.State != null or logRecord.StateValues will be called.
6664 if ( logRecord . State != null )
6765 {
68- if ( logRecord . State is IReadOnlyCollection < KeyValuePair < string , object > > stateDictionary )
66+ if ( logRecord . State is IReadOnlyCollection < KeyValuePair < string , object ? > > stateDictionary )
6967 {
7068 ExtractProperties ( ref message , properties , stateDictionary ) ;
7169 }
@@ -93,14 +91,14 @@ internal static string GetMessageAndSetProperties(LogRecord logRecord, IDictiona
9391
9492 internal static void WriteScopeInformation ( LogRecord logRecord , IDictionary < string , string > properties )
9593 {
96- StringBuilder builder = null ;
94+ StringBuilder ? builder = null ;
9795 int originalScopeDepth = 1 ;
9896 logRecord . ForEachScope ( ProcessScope , properties ) ;
9997
10098 void ProcessScope ( LogRecordScope scope , IDictionary < string , string > properties )
10199 {
102100 int valueDepth = 1 ;
103- foreach ( KeyValuePair < string , object > scopeItem in scope )
101+ foreach ( KeyValuePair < string , object ? > scopeItem in scope )
104102 {
105103 if ( string . IsNullOrEmpty ( scopeItem . Key ) )
106104 {
@@ -109,7 +107,7 @@ void ProcessScope(LogRecordScope scope, IDictionary<string, string> properties)
109107 }
110108 else if ( scopeItem . Key == "{OriginalFormat}" )
111109 {
112- properties . Add ( $ "OriginalFormatScope_{ s_depthCache . GetOrAdd ( originalScopeDepth , s_convertDepthToStringRef ) } ", Convert . ToString ( scope . Scope . ToString ( ) , CultureInfo . InvariantCulture ) ) ;
110+ properties . Add ( $ "OriginalFormatScope_{ s_depthCache . GetOrAdd ( originalScopeDepth , s_convertDepthToStringRef ) } ", Convert . ToString ( scope . Scope ? . ToString ( ) , CultureInfo . InvariantCulture ) ) ;
113111 }
114112 else if ( ! properties . TryGetValue ( scopeItem . Key , out _ ) )
115113 {
@@ -138,8 +136,8 @@ internal static string GetProblemId(Exception exception)
138136 int methodOffset = System . Diagnostics . StackFrame . OFFSET_UNKNOWN ;
139137
140138 var exceptionType = exception . GetType ( ) . FullName ;
141- var strackTrace = new StackTrace ( exception ) ;
142- var exceptionStackFrame = strackTrace . GetFrame ( 0 ) ;
139+ var stackTrace = new StackTrace ( exception ) ;
140+ var exceptionStackFrame = stackTrace . GetFrame ( 0 ) ;
143141
144142 if ( exceptionStackFrame != null )
145143 {
@@ -187,27 +185,30 @@ internal static SeverityLevel GetSeverityLevel(LogLevel logLevel)
187185 }
188186 }
189187
190- private static void ExtractProperties ( ref string message , IDictionary < string , string > properties , IReadOnlyCollection < KeyValuePair < string , object > > stateDictionary )
188+ private static void ExtractProperties ( ref string ? message , IDictionary < string , string > properties , IReadOnlyCollection < KeyValuePair < string , object ? > > stateDictionary )
191189 {
192- foreach ( KeyValuePair < string , object > item in stateDictionary )
190+ foreach ( KeyValuePair < string , object ? > item in stateDictionary )
193191 {
194- if ( item . Key == "{OriginalFormat}" )
192+ if ( item . Key . Length <= SchemaConstants . KVP_MaxKeyLength && item . Value != null )
195193 {
196- if ( message == null )
194+ // Note: if Key exceeds MaxLength, the entire KVP will be dropped.
195+
196+ if ( item . Key == "{OriginalFormat}" )
197197 {
198- message = item . Value . ToString ( ) ;
198+ if ( message == null )
199+ {
200+ message = item . Value . ToString ( ) ;
201+ }
202+ else
203+ {
204+ properties . Add ( "OriginalFormat" , item . Value . ToString ( ) . Truncate ( SchemaConstants . KVP_MaxValueLength ) ) ;
205+ }
199206 }
200207 else
201208 {
202- properties . Add ( "OriginalFormat" , item . Value . ToString ( ) . Truncate ( SchemaConstants . KVP_MaxValueLength ) ) ;
209+ properties . Add ( item . Key , item . Value . ToString ( ) . Truncate ( SchemaConstants . KVP_MaxValueLength ) ) ;
203210 }
204211 }
205- else if ( item . Key . Length <= SchemaConstants . KVP_MaxKeyLength )
206- {
207- // Note: if Key exceeds MaxLength, the entire KVP will be dropped.
208-
209- properties . Add ( item . Key , item . Value . ToString ( ) . Truncate ( SchemaConstants . KVP_MaxValueLength ) ) ;
210- }
211212 }
212213 }
213214
0 commit comments