@@ -23,7 +23,7 @@ namespace Microsoft.ApplicationInsights.NLogTarget
2323 /// The messages will be uploaded to the Application Insights cloud service.
2424 /// </summary>
2525 [ Target ( "ApplicationInsightsTarget" ) ]
26- public sealed class ApplicationInsightsTarget : TargetWithLayout
26+ public sealed class ApplicationInsightsTarget : TargetWithContext
2727 {
2828 private const string ConnectionStringRequiredMessage = "Azure Monitor connection string is required. Please provide a valid connection string." ;
2929
@@ -39,6 +39,7 @@ public ApplicationInsightsTarget()
3939 {
4040 this . Layout = @"${message}" ;
4141 this . OptimizeBufferReuse = true ;
42+ this . IncludeEventProperties = true ;
4243 }
4344
4445 /// <summary>
@@ -50,12 +51,6 @@ public string ConnectionString
5051 set => this . connectionStringLayout = value ?? string . Empty ;
5152 }
5253
53- /// <summary>
54- /// Gets the array of custom attributes to be passed into the logevent context.
55- /// </summary>
56- [ ArrayParameter ( typeof ( TargetPropertyWithContext ) , "contextproperty" ) ]
57- public IList < TargetPropertyWithContext > ContextProperties { get ; } = new List < TargetPropertyWithContext > ( ) ;
58-
5954 /// <summary>
6055 /// Gets the logging controller we will be using.
6156 /// </summary>
@@ -89,20 +84,25 @@ internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
8984 propertyBag . Add ( "UserStackFrame" , logEvent . UserStackFrame . ToString ( ) ) ;
9085 propertyBag . Add ( "UserStackFrameNumber" , logEvent . UserStackFrameNumber . ToString ( CultureInfo . InvariantCulture ) ) ;
9186 }
92-
93- for ( int i = 0 ; i < this . ContextProperties . Count ; ++ i )
87+ else
9488 {
95- var contextProperty = this . ContextProperties [ i ] ;
96- if ( ! string . IsNullOrEmpty ( contextProperty . Name ) && contextProperty . Layout != null )
97- {
98- string propertyValue = this . RenderLogEvent ( contextProperty . Layout , logEvent ) ;
99- PopulatePropertyBag ( propertyBag , contextProperty . Name , propertyValue ) ;
100- }
89+ var callsiteClassName = logEvent . CallerClassName ;
90+ if ( ! string . IsNullOrEmpty ( callsiteClassName ) )
91+ propertyBag . Add ( "UserStackClassName" , callsiteClassName ) ;
92+ var callsiteMemberName = logEvent . CallerMemberName ;
93+ if ( ! string . IsNullOrEmpty ( callsiteMemberName ) )
94+ propertyBag . Add ( "UserStackMemberName" , callsiteMemberName ) ;
95+ var callsiteSourceFilePath = logEvent . CallerFilePath ;
96+ if ( ! string . IsNullOrEmpty ( callsiteSourceFilePath ) )
97+ propertyBag . Add ( "UserStackSourceFile" , callsiteSourceFilePath ) ;
98+ var callsiteSourceLineNumber = logEvent . CallerLineNumber ;
99+ if ( callsiteSourceLineNumber != 0 )
100+ propertyBag . Add ( "UserStackSourceLine" , callsiteSourceLineNumber . ToString ( ) ) ;
101101 }
102102
103- if ( logEvent . HasProperties )
103+ if ( ShouldIncludeProperties ( logEvent ) || ContextProperties . Count > 0 )
104104 {
105- LoadLogEventProperties ( logEvent , propertyBag ) ;
105+ this . GetAllProperties ( logEvent , new StringDictionaryConverter ( propertyBag ) ) ;
106106 }
107107 }
108108
@@ -222,40 +222,6 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation)
222222 }
223223 }
224224
225- private static void LoadLogEventProperties ( LogEventInfo logEvent , IDictionary < string , string > propertyBag )
226- {
227- if ( logEvent . Properties ? . Count > 0 )
228- {
229- foreach ( var keyValuePair in logEvent . Properties )
230- {
231- string key = keyValuePair . Key . ToString ( ) ;
232- object valueObj = keyValuePair . Value ;
233- PopulatePropertyBag ( propertyBag , key , valueObj ) ;
234- }
235- }
236- }
237-
238- private static void PopulatePropertyBag ( IDictionary < string , string > propertyBag , string key , object valueObj )
239- {
240- if ( valueObj == null )
241- {
242- return ;
243- }
244-
245- string value = Convert . ToString ( valueObj , CultureInfo . InvariantCulture ) ;
246- if ( propertyBag . ContainsKey ( key ) )
247- {
248- if ( string . Equals ( value , propertyBag [ key ] , StringComparison . Ordinal ) )
249- {
250- return ;
251- }
252-
253- key += "_1" ;
254- }
255-
256- propertyBag . Add ( key , value ) ;
257- }
258-
259225 private static SeverityLevel ? GetSeverityLevel ( LogLevel logEventLevel )
260226 {
261227 if ( logEventLevel == null )
0 commit comments