@@ -26,7 +26,7 @@ namespace Microsoft.ApplicationInsights.NLogTarget
2626 /// The messages will be uploaded to the Application Insights cloud service.
2727 /// </summary>
2828 [ Target ( "ApplicationInsightsTarget" ) ]
29- public sealed class ApplicationInsightsTarget : TargetWithLayout
29+ public sealed class ApplicationInsightsTarget : TargetWithContext
3030 {
3131 private TelemetryClient telemetryClient ;
3232 private DateTime lastLogEventTime ;
@@ -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 InstrumentationKey
5051 set => this . instrumentationKeyLayout = 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>
@@ -90,20 +85,25 @@ internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace)
9085 propertyBag . Add ( "UserStackFrame" , logEvent . UserStackFrame . ToString ( ) ) ;
9186 propertyBag . Add ( "UserStackFrameNumber" , logEvent . UserStackFrameNumber . ToString ( CultureInfo . InvariantCulture ) ) ;
9287 }
93-
94- for ( int i = 0 ; i < this . ContextProperties . Count ; ++ i )
88+ else
9589 {
96- var contextProperty = this . ContextProperties [ i ] ;
97- if ( ! string . IsNullOrEmpty ( contextProperty . Name ) && contextProperty . Layout != null )
98- {
99- string propertyValue = this . RenderLogEvent ( contextProperty . Layout , logEvent ) ;
100- PopulatePropertyBag ( propertyBag , contextProperty . Name , propertyValue ) ;
101- }
90+ var callsiteClassName = logEvent . CallerClassName ;
91+ if ( ! string . IsNullOrEmpty ( callsiteClassName ) )
92+ propertyBag . Add ( "UserStackClassName" , callsiteClassName ) ;
93+ var callsiteMemberName = logEvent . CallerMemberName ;
94+ if ( ! string . IsNullOrEmpty ( callsiteMemberName ) )
95+ propertyBag . Add ( "UserStackMemberName" , callsiteMemberName ) ;
96+ var callsiteSourceFilePath = logEvent . CallerFilePath ;
97+ if ( ! string . IsNullOrEmpty ( callsiteSourceFilePath ) )
98+ propertyBag . Add ( "UserStackSourceFile" , callsiteSourceFilePath ) ;
99+ var callsiteSourceLineNumber = logEvent . CallerLineNumber ;
100+ if ( callsiteSourceLineNumber != 0 )
101+ propertyBag . Add ( "UserStackSourceLine" , callsiteSourceLineNumber . ToString ( ) ) ;
102102 }
103103
104- if ( logEvent . HasProperties )
104+ if ( ShouldIncludeProperties ( logEvent ) || ContextProperties . Count > 0 )
105105 {
106- LoadLogEventProperties ( logEvent , propertyBag ) ;
106+ this . GetAllProperties ( logEvent , new StringDictionaryConverter ( propertyBag ) ) ;
107107 }
108108 }
109109
@@ -182,40 +182,6 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation)
182182 }
183183 }
184184
185- private static void LoadLogEventProperties ( LogEventInfo logEvent , IDictionary < string , string > propertyBag )
186- {
187- if ( logEvent . Properties ? . Count > 0 )
188- {
189- foreach ( var keyValuePair in logEvent . Properties )
190- {
191- string key = keyValuePair . Key . ToString ( ) ;
192- object valueObj = keyValuePair . Value ;
193- PopulatePropertyBag ( propertyBag , key , valueObj ) ;
194- }
195- }
196- }
197-
198- private static void PopulatePropertyBag ( IDictionary < string , string > propertyBag , string key , object valueObj )
199- {
200- if ( valueObj == null )
201- {
202- return ;
203- }
204-
205- string value = Convert . ToString ( valueObj , CultureInfo . InvariantCulture ) ;
206- if ( propertyBag . ContainsKey ( key ) )
207- {
208- if ( string . Equals ( value , propertyBag [ key ] , StringComparison . Ordinal ) )
209- {
210- return ;
211- }
212-
213- key += "_1" ;
214- }
215-
216- propertyBag . Add ( key , value ) ;
217- }
218-
219185 private static SeverityLevel ? GetSeverityLevel ( LogLevel logEventLevel )
220186 {
221187 if ( logEventLevel == null )
0 commit comments